I need to convert a date from this format:
2002-10-10T12:00:00-05:00 (xs:dateTime as defined in XML)
to an Oracle date.
Oracle dates don't have timezone information. You'll need to use a TIMESTAMP datatype instead.
It works something like this:
SQL> desc tz
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
TS TIMESTAMP(6) WITH TIME ZONE
TNOW TIMESTAMP(6) WITH TIME ZONE
SQL> insert into tz
2 values (1
3 , to_timestamp_tz('2002-10-10 12:00:00-05:00'
4 , 'YYYY-MM-DD HH24:MI:SSTZH:TZM')
5 , systimestamp)
6 /
1 row created.
SQL> select * from tz
2 /
ID
----------
TS
---------------------------------------------------------------------------
TNOW
---------------------------------------------------------------------------
1
10-OCT-02 12.00.00.000000 -05:00
23-AUG-10 17.37.06.502000 +01:00
SQL>
Note, there is the tricky issue of the T in the XSD notation. That hurls a ORA-01858
exception, because it's not a valid format in Oracle. I'm sure there is a workaround, but it currently escapes me.
Well, one workaround is to apply SUBSTR() function sto split open the two parts of the timestamp, as Bob shows. But there ought to be a more elegant way.
It probably doesn't qualify as "elegant" but as it's a string we can use a substitution function to get rid of the annoying T:
SQL> insert into tz
2 values (2
3 , to_timestamp_tz(translate('2003-10-10T12:00:00-05:00', 'T', ' ')
4 , 'YYYY-MM-DD HH24:MI:SSTZH:TZM')
5 , systimestamp)
6 /
1 row created.
SQL> select * from tz
2 /
ID
----------
TS
---------------------------------------------------------------------------
TNOW
---------------------------------------------------------------------------
1
10-OCT-02 12.00.00.000000 -05:00
23-AUG-10 17.37.06.502000 +01:00
2
10-OCT-03 12.00.00.000000 -05:00
23-AUG-10 17.53.37.113000 +01:00
SQL>
But given all the effort Oracle have put into XMLDB it is rather annoying that there isn't a tidier solution.
"I dont understand how you get -05:00."
In my original sample I use a format mask of 'YYYY-MM-DD HH24:MI:SS-TZH:TZM'
. This interprets the -
in the time zone as a separator not a minus sign. Consequently it returned +05:00. I have since corrected my code sample to remove that last dash. Now the timezone is correctly rendered as -05:00. Sorry for any confusion.