The following query succeeds:
SELECT INTERVAL '00:00:00.000000' HOUR TO SECOND(6)
FROM DUAL;
The following fails:
SELECT INTERVAL time_field HOUR TO SECOND(6) -- time_field is a VARCHAR2(15)
FROM some_table;
w/ the following error:
ORA-00923: FROM keyword not found where expected
How do I fix my second query to pull back an HOUR TO SECOND INTERVAL?
When I try:
SELECT cast(time_field AS INTERVAL HOUR TO SECOND(6))
FROM some_table
I get the following:
ORA-00963: unsupported interval type
TEST SETUP:
CREATE TABLE some_table (
time_field VARCHAR2(50)
);
INSERT ALL
INTO some_table (time_field) VALUES ('10:00:00.000000')
INTO some_table (time_field) VALUES ('12:00:00.000000')
INTO some_table (time_field) VALUES ('15:00:00.000000')
INTO some_table (time_field) VALUES ('17:00:00.000000')
INTO some_table (time_field) VALUES ('20:00:00.000000')
INTO some_table (time_field) VALUES (NULL)
SELECT * FROM DUAL;
You can use the
TO_DSINTERVALfunction; example here using a CTE to replicate your table:Note that to make the format something the function recognises, you have to supply a ‘days’ part, hence the prepending of the dummy
'0 'string.Your fixed-value query returns a slightly different format (or at least displays slightly differently; interval daa types do not have format models in quite the same way as dates, so not sure how to express that accurately):
To replicate that you can
castthis if you need to:… or just:
… which is pretty much what @catcall first suggested, but this also needs the
'0 'prepended, or you get anORA-01867. (Or, if you try to useHOUR TO SECOND,ORA-00963, even with the prepended day value). However, I guess (and it is just a guess, though very slightly educated) that it’s doing an implicitTO_DSINTERVALor similar, and I think I’d prefer to use an explicit one so I was sure what’s happening. That might just be me, though…Using your sample data I too get an
ORA-01867, but it’s caused by the null value. You can use acaseto leave that as null in the result: