Oracle SQL query statement and conditions with timestamps and ISO dates

回眸只為那壹抹淺笑 提交于 2019-11-29 16:25:20
Alex Poole

Based on an earlier question, it's tempting to treat both the T and Z as character literals, and basically ignore them, using:

to_timestamp_tz('2014-01-28T12:00:0Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')

If you use to_timestamp_tz() without specifying a timezone then it defaults to your session time zone, as would to_timestamp(); so a time specified in Zulu/UTC loses that zone information:

alter session set time_zone = 'America/New_York';
select to_timestamp_tz('2014-01-28T12:00:0Z',
  'YYYY-MM-DD"T"HH24:MI:SS"Z"') from dual;

TO_TIMESTAMP_TZ('2014-01-28T12:00:0Z','YYYY-MM-DD"T"HH24:MI:SS"Z"')
-------------------------------------------------------------------
28-JAN-14 12.00.00.000000000 AMERICA/NEW_YORK                       

Your 12:00 time is shown as 12:00 in New York, not 12:00 UTC.

A safer conversion, assuming your values are always supposed to represent UTC, is to specify the time zone explicitly with the from_tz() function:

WHERE MODIFICATION_DATE >= from_tz(to_timestamp('2014-01-28T12:00:0Z',
  'YYYY-MM-DD"T"HH24:MI:SS"Z"'), 'UTC')

This gets the UTC time properly:

alter session set time_zone = 'America/New_York';
select from_tz(to_timestamp('2014-01-28T12:00:0Z',
  'YYYY-MM-DD"T"HH24:MI:SS"Z"'), 'UTC') from dual;

FROM_TZ(TO_TIMESTAMP('2014-01-28T12:00:0Z','YYYY-MM-DD"T"HH24:MI:SS"Z"'),'UTC')
-------------------------------------------------------------------------------
28-JAN-14 12.00.00.000000000 UTC                                                
Sathyajith Bhat

Look at the to_timestamp_tz function.

Assuming Z is for Zulu/UTC, you ca probably do a replace of Z with +00:00 and work with to_timestamp_tz function

SELECT * FROM table_name 
WHERE modification_date >= to_timestamp_tz(replace('2014-01-28T12:00:00z','z','+00:00'),'YYYY-MM-DD"T"hh24:mi:sstzh:tzm')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!