Comparing TIMESTAMP WITH TIME ZONE to DATE - oracle

I need to compare database generated dates (column values defaulting to SYSDATE) to hand written time stamps recorded with the time zone. This is the comparison I am trying:
where trunc(updated, 'mi') >= to_timestamp_tz('2017-10-24 04:45 US/Pacific', 'YYYY-MM-DD HH24:mi TZR')
I assume that a character string converted to a TIMESTAMP WITH TIME ZONE should be comparable to a DATE. However, this only works if the database is located in my own time zone. Otherwise, I have to manually convert the external time stamp to the database time zone. For example, if I am in BST and the database is in EST, I have to write:
where trunc(updated, 'mi') >= to_timestamp_tz('2017-10-24 04:45 US/Pacific', 'YYYY-MM-DD HH24:mi TZR')
Where 04:45 = 10:45 - 6, and 6 is the difference between BST and EST. This looks absolutely counter-intuitive as the original time stamp has been recorded in PST and is therefore entered as US/Pacific. Could anybody please explain why this conversion is needed? I would also appreciate if somebody suggests a better solution.

You can cast your DATE column to a TIMESTAMP WITH TIME ZONE value like this:
WHERE
FROM_TZ(CAST(TRUNC(updated, 'mi') AS TIMESTAMP),
(SELECT TO_CHAR(SYSTIMESTAMP, 'TZR') FROM dual)) >= to_timestamp_tz('2017-10-24 04:45 US/Pacific', 'YYYY-MM-DD HH24:mi TZR')
but it works also the other way around, i.e. convert TIMESTAMP WITH TIME ZONE to DATE in database time zone:
WHERE
TRUNC(updated, 'mi') >=
CAST(TO_TIMESTAMP_TZ('2017-10-24 04:45 US/Pacific', 'YYYY-MM-DD HH24:mi TZR') AT TIME ZONE (SELECT TO_CHAR(SYSTIMESTAMP, 'TZR') FROM dual) AS DATE)
SYDATE is provided in the time zone of database server's operating system (NOT DBTIMEZONE) , thus you have to use (SELECT TO_CHAR(SYSTIMESTAMP, 'TZR') FROM dual) or provide hard-coded value if appropriate.
This approach fails if any users inserted/updated any updated using his current local time zone. In such cases the time zone information is lost and there is no way to recover it.

If you are running this query in any of oracle clients, You can also change the session setting by running
ALTER SESSION SET TIME_ZONE ='BST';
When you change the session time_zone all the date values coming from database column and time stamp values passed without time zone will be converted to BST.
hence it will ensure that the comparison is happening in common time zone

Related

Determine if spring or fall and adjust time if DST or not

I have a query like this:
SELECT
to_date(to_char((from_tz(to_timestamp(to_char(my_column, 'YYYY-MM-DD HH:MI:SS PM'), 'YYYY-MM-DD HH:MI:SS PM') ,'America/Edmonton')at time zone 'America/Vancouver'),'YYYY-MM-DD'),'YYYY-MM-DD') as Date_Column
FROM
my_table
It converts the time between MST to PST. This works most of the time except when daylight savings time occurs. Then a situation happens when in the clock moves forward an hour, and then since an hour is lost (for example 1:59 to the 3:00 am) then we are trying to convert a fictional time.
So I am aware of the issue, and I know Oracle will throw this error for that reason:
ORA-01878: specified field not found in datetime or interval
I've looked all over the Internet for a possible work around but can't really find anything that works. I just want to modify my query so that it can tell what time of year it is and either do the conversation when the time exists, or handle the time somehow when it does not exists. Perhaps add the extra hour when it's missing, and remove it when it's the other time of year.
Does anyone have a solution for this? Is this an impossible problem?
Instead of all the conversions you are using, just use AT TIME ZONE. You may need to take extra care depending on the data type of your column. The AT TIME ZONE functionality will always take into account daylights savings time.
If your column is a TIMESTAMP WITH TIME ZONE, that would be ideal
because you could just say AT TIME ZONE 'PST' and you would get the
time in PST.
If the column is the datatype TIMESTAMP, when you use AT TIME ZONE it
will convert from the time zone of the current session to whatever
you specify.
If your column is a DATE, you will need to convert it to a TIMESTAMP first, then you can use the AT TIME ZONE function. Converting the DATE to TIMESTAMP will make the time zone 0:00 offset so be sure to account for that.
Another handy trick is to use AT LOCAL. This will convert the timestamp to the time zone of the current session. This may be handy if you have users of your application that are in multiple time zones.
Example
I am in the time zone -04:00 (East coast US) which is why that appears as my time zone in the examples below.
WITH
my_table
AS
(SELECT TIMESTAMP '2021-01-01 8:00:00 -7:00' AS ts_with_tz,
TIMESTAMP '2021-01-01 8:00:00' AS ts,
DATE '2021-01-01' + (8 / 24) AS dt
FROM DUAL)
SELECT 1 AS example_num,
t.ts_with_tz,
t.ts,
t.dt
FROM my_table t
UNION ALL
SELECT 2 AS example_num,
t.ts_with_tz AT TIME ZONE 'PST',
t.ts AT TIME ZONE 'PST',
TO_TIMESTAMP (t.dt) AT TIME ZONE 'PST'
FROM my_table t
ORDER BY 1;
RESULT
EXAMPLE_NUM TS_WITH_TZ TS DT
______________ ______________________________________________________ ______________________________________________________ ______________________________________________________
1 01-JAN-21 08.00.00.000000000 AM -07:00 01-JAN-21 08.00.00.000000000 AM AMERICA/NEW_YORK 01-JAN-21 08.00.00.000000000 AM AMERICA/NEW_YORK
2 01-JAN-21 07.00.00.000000000 AM AMERICA/LOS_ANGELES 01-JAN-21 05.00.00.000000000 AM AMERICA/LOS_ANGELES 31-DEC-20 09.00.00.000000000 PM AMERICA/LOS_ANGELES

How to compare timestamp(6) columns with systimestamp in oracle

I want to compare the timestamp columns values with the systimestamp value.
My table contains two columns from_date and to_date with datatype of timestamp(6). I want to check something like this
systimestamp between from_date and thru_date
Upon checking systimestamp is returning value with timezone but from_date and thru_date columns values are without timezone
select systimestamp from dual; -- 11/19/2016 03:35:55.042420000 PM +08:00
And from_date & thru_date like 08/03/2015 06:09:56.941255000 AM
Please advise me how can I achieve this comparison?
Can I use LOCALTIMESTAMP instead of SYSTIMESTAMP? or I have to use from_tz function on from_date and to_date columns before comparison?
For a comparison Oracle has to perform an implicit cast. I just made a test and Oracle runs internally
systimestamp between CAST(from_date AS TIMESTAMP WITH TIME ZONE) and CAST(thru_date AS TIMESTAMP WITH TIME ZONE),
which is equivalent to
systimestamp between FROM_TZ(from_date, SESSIONTIMEZONE) and FROM_TZ(thru_date, SESSIONTIMEZONE)
In case your current SESSIONTIMEZONE is different from time zone of database server's operating system the query will return wrong result.
In order to get correct result you can use either of these expressions:
CAST(systimestamp AS TIMESTAMP) between from_date and thru_date
systimestamp between FROM_TZ(from_date, EXTRACT(TIMEZONE_REGION FROM SYSTIMESTAMP))
and FROM_TZ(thru_date, EXTRACT(TIMEZONE_REGION FROM SYSTIMESTAMP))
systimestamp between FROM_TZ(from_date, TO_CHAR(SYSTIMESTAMP, 'TZR'))
and FROM_TZ(thru_date, TO_CHAR(SYSTIMESTAMP, 'TZR'))
systimestamp between FROM_TZ(from_date, TO_CHAR(SYSTIMESTAMP, 'TZH:TZM'))
and FROM_TZ(thru_date, TO_CHAR(SYSTIMESTAMP, 'TZH:TZM'))
EXTRACT(TIMEZONE_REGION FROM SYSTIMESTAMP) may fail, depending on your server settings.
Note, do not use DBTIMEZONE instead of TO_CHAR(SYSTIMESTAMP, 'TZR'). DBTIMEZONE can be different from time zone of database server's operating system. Time zone of SYSTIMESTAMP is given in time zone of database server's operating system not DBTIMEZONE!

Issue with timezone in oracle

Below query is used in my code to get the timezone from database. (DB is in central time zone)
SELECT DBTIMEZONE FROM DUAL;
-05:00
When daylight saving is on (CDT), its expected result.
but when daylight saving ends (CST), my result should be GMT-06, but i am still getting GMT-05.
Googled and got below options :
SELECT TO_CHAR(SYSTIMESTAMP, 'TZR') FROM dual;
SELECT TZ_OFFSET('CST6CDT') FROM DUAL;
Will above queries run correctly accordingly to CST and CDT ??
DBTIMEZONE does not determine the time zone of SYSTIMESTAMP (or SYSDATE)
Time zone of SYSTIMESTAMP is the time zone of database server's operating system.
DBTIMEZONE is only relevant for data type TIMESTAMP WITH LOCAL TIME ZONE and defines the time zone in which those values are stored internaly. You cannot change DBTIMEZONE on your database if the database contains a table with a TIMESTAMP WITH LOCAL TIME ZONE column and the column contains data.

How to convert date object to timestamp in client?

for example
select to_timestamp(sysdate) from dual
return date object, not timestamp.
I try to change
NLS_TIMESTAMP_FORMAT='ss.ff'
but select return error.
If you are starting with sysdate then as #a_horse_with_no_name says you don't need to do a conversrion; use systimestamp or current_timestamp instead. (One is the server time, one is the client time, which will be the same unless your client is in a different timezone).
More generally though you can cast between data types:
select cast(date_field as timestamp) from your_table
You won't add any precision to the value though; the date already have a time down to second precision, even if that is midnight; and your timestamp will still have the fractional seconds part as zero.
If you just want to display your DATE as a string but show the time it already has then you need to specify the output format, e.g.
select to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS') from dual

How to get UTC value for SYSDATE on Oracle

Probably a classic... Would you know a easy trick to retrieve an UTC value of SYSDATE on Oracle (best would be getting something working on the 8th version as well).
For now I've custom function :(
Cheers,
Stefan
You can use
SELECT SYS_EXTRACT_UTC(TIMESTAMP '2000-03-28 11:30:00.00 -02:00') FROM DUAL;
You may also need to change your timezone
ALTER SESSION SET TIME_ZONE = 'Europe/Berlin';
Or read it
SELECT SESSIONTIMEZONE, CURRENT_TIMESTAMP FROM dual;
select sys_extract_utc(systimestamp) from dual;
Won't work on Oracle 8, though.
Usually, I work with DATE columns, not the larger but more precise TIMESTAMP used by some answers.
The following will return the current UTC date as just that -- a DATE.
CAST(sys_extract_utc(SYSTIMESTAMP) AS DATE)
I often store dates like this, usually with the field name ending in _UTC to make it clear for the developer. This allows me to avoid the complexity of time zones until last-minute conversion by the user's client. Oracle can store time zone detail with some data types, but those types require more table space than DATE, and knowledge of the original time zone is not always required.
I'm using:
SELECT CAST(SYSTIMESTAMP AT TIME ZONE 'UTC' AS DATE) FROM DUAL;
It's working fine for me.
If you want a timestamp instead of just a date with sysdate, you can specify a timezone using systimestamp:
select systimestamp at time zone 'UTC' from dual
outputs: 29-AUG-17 06.51.14.781998000 PM UTC

Resources