简体   繁体   中英

TO_DATE Recognise date

Ok, I have this date string:

2016-05-31T23:00:00.000Z

I want to be able to use it to search on a date in an Oracle 12c table, for example

SELECT stuff 
FROM TABLE 
WHERE date_column > TO_DATE('2016-05-31T23:00:00.000Z', 'what goes here?');

I can't figure out what format this date is in, can anyone help? This is probably simple, but I can't seem to find it...

Edit: this isn't C#

If you need the string representing UTC converted to your local time zone then you need to do a few steps. The starting point is to use to_timestamp() with character literals for the T and Z, which Oracle doesn't recognise:

select to_timestamp('2016-05-31T23:00:00.000Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
from dual;

TO_TIMESTAMP('2016-05-31T23:00:00.000Z','YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
-------------------------------------------------------------------------
2016-05-31 23:00:00.000                                                  

Then you can state that timezone-less value is actually UTC with from_tz() :

select from_tz(
  to_timestamp('2016-05-31T23:00:00.000Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"'),
  'UTC')
from dual;

FROM_TZ(TO_TIMESTAMP('2016-05-31T23:00:00.000Z','YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"'
--------------------------------------------------------------------------------
2016-05-31 23:00:00.000 UTC                                                     

Then you can convert it to your own time zone:

select from_tz(
  to_timestamp('2016-05-31T23:00:00.000Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"'),
  'UTC') at time zone 'Europe/London'
from dual;

FROM_TZ(TO_TIMESTAMP('2016-05-31T23:00:00.000Z','YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"'
--------------------------------------------------------------------------------
2016-06-01 00:00:00.000 EUROPE/LONDON                                           

If you want it back as a date datatype you can cast it:

select cast(from_tz(
  to_timestamp('2016-05-31T23:00:00.000Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"'),
  'UTC') at time zone 'Europe/London' as date)
from dual;

CAST(FROM_TZ(TO_TIMESTAMP('2016-05-31T23:00:00.000Z','YYYY-MM-DD"T"HH24:MI:SS.FF
--------------------------------------------------------------------------------
2016-06-01 00:00:00                                                             

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM