TO_DATE Recognise date

时光毁灭记忆、已成空白 提交于 2020-06-18 11:49:32

问题


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#


回答1:


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                                                             


来源:https://stackoverflow.com/questions/37861985/to-date-recognise-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!