Convert String ISO-8601 date to oracle's timestamp datatype
Asked Answered
D

2

17

I have a ISO-8601 date in VARCHAR2 type, how can i convert that String date to timestamp in oracle db?

Date Example: "2014-09-12T11:53:06+00:00"

Maybe is something like the following but i not sure what is the format.

SELECT to_timestamp_tz ('2014-09-12T11:53:06+00:00', ????) FROM DUAL
Dann answered 31/10, 2014 at 9:43 Comment(0)
B
25

The date format model elements are listed in the Datetime Format Models documentation:

SELECT to_timestamp_tz ('2014-09-12T11:53:06+00:00', 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
FROM DUAL

TO_TIMESTAMP_TZ('2014-09-12T11:53:06+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
---------------------------------------------------------------------------
12-SEP-14 11.53.06.000000000 +00:00

The fixed T can be included as a character literal:

You can include these characters in a date format model:

  • Punctuation such as hyphens, slashes, commas, periods, and colons
  • Character literals, enclosed in double quotation marks

TZH is tome zone hour, and TZM is time zone minutes. The rest are more common model elements.

Bohun answered 31/10, 2014 at 9:49 Comment(0)
J
-1

I had an app that was making queries against an OracleDB database (19c). The app was sending timestamp parameters in ISO 8601 format and the table had a column that was an Oracle TIMESTAMP type (not an Oracle TIMESTAMP WITH TIMEZONE type) that was just known to store timestamps that represented UTC time.

I thought the following query would work but it didn't seem to recognize the literals that were surrounded by double quotes:

SELECT * FROM measurements
WHERE measID = '333'
AND measTime > to_timestamp('2020-11-19T05:00:00Z', 'YYYY-MM-DD"T"hh:mm:ss"Z"');
-- Doesn't Work
-- ORA-01810: format code appears twice
-- 01810. 00000 -  "format code appears twice"

The following did work for me:

SELECT * FROM measurements
WHERE measID = '333'
AND measTime > SYS_EXTRACT_UTC(TO_UTC_TIMESTAMP_TZ('2020-11-19T05:00:00Z'));
-- Works

Sharing because formatting ISO 8601 to TIMESTAMP types wasn't immediately clear in Oracle documentation. I found this solution here: https://oracle-base.com/articles/18c/to_utc_timestamp_tz-function-18c

Jounce answered 19/11, 2020 at 17:1 Comment(2)
You had a typo ...YYYY-MM-DD"T"hh:mm:ss"Z"... should be ...YYYY-MM-DD"T"hh:mi:ss"Z"... Notice the mm -> mi for minutes.Tapioca
Also, replace hh with hh24 to get for instance 19 instead of 07 (pm).Homocentric

© 2022 - 2024 — McMap. All rights reserved.