I need a query which will select records from Oracle database on the basis of the datetime condition. Example below:
SELECT * FROM table_name WHERE [modification_date] >= '2014-01-28T12:00:00Z';
As datetime I used ISO date and this is "Must be". In Oracle XE database a [modification_date] column has type "Timestamp with time zone".
And now is my question - How to convert ISO date in query to proper searching on database ?
I tried adding to_timestamp_tz to query statement.
SELECT * FROM table_name
WHERE MODIFICATION_DATE >= to_timestamp_tz('2014-01-28T00:00:0Z', 'YYYY-MM-DD"T"HH24:MI:SS');
But get this error:
SQL Error [1830] [22008]: ORA-01830: date format picture ends before
converting entire input string
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
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')