Change Oracle ADW default timezone - oracle

The ADW is configured for UTC timezone and there is need to change it to EST timezone.
The ETL custom application has many stored custom procedures which SYSDATE returns the current date and time set for the operating system.
What is the best approach to solve this problem in ADW?

To change timezone please refer here.
Note that this does not change the output of SYSDATE as that relies on the OS timezone which cannot be changed. The recommendation is to use CURRENT_DATE instead.
Upgrade Example
This example illustrates updating DST behavior to Oracle Database 11g,
release 2 for which the default time zone version is 14. First, assume
that your current database is using time zone version 3, and also
assume you have an existing table t, which contains timestamp with
time zone data.
Connect to the database as the user scott and execute the following
statements:
DROP TABLE t;
CREATE TABLE t (c NUMBER, mark VARCHAR(25), ts TIMESTAMP WITH TIME ZONE);
INSERT INTO t VALUES(1, 'not_affected',
to_timestamp_tz('22-sep-2006 13:00:00 america/los_angeles',
'dd-mon-yyyy hh24:mi:ss tzr tzd'));
INSERT INTO t VALUES(4, 'affected_err_exist',
to_timestamp_tz('11-mar-2007 00:30:00 america/st_johns',
'dd-mon-yyyy hh24:mi:ss tzr tzd'));
INSERT INTO t VALUES(6, 'affected_no_err',
to_timestamp_tz('11-mar-2007 01:30:00 america/st_johns',
'dd-mon-yyyy hh24:mi:ss tzr tzd'));
INSERT INTO t VALUES(14, 'affected_err_dup',
to_timestamp_tz('21-sep-2006 23:30:00 egypt',
'dd-mon-yyyy hh24:mi:ss tzr tzd'));
COMMIT;
Then, optionally, you can start a prepare window to check the affected
data and potential semantic errors where there is an overlap or
non-existing time. To do this, you should start a window for
preparation to migrate to time zone version 14. It is assumed that you
have the necessary privileges. These privileges are controlled with
the DBMS_DST package.

Related

Oracle server timezone using SQL query

I ran
select SYSDATE from dual;
Output:
SYSDATE |
-------------------|
2019-10-09 08:55:29|
Then I ran,
SELECT DBTIMEZONE FROM DUAL;
Output:
DBTIMEZONE|
----------|
+00:00 |
In the first output, time is in EST and 2nd output suggests timezone is UTC.
How do I check oracle server timezone via SQL query?
From the docs:
The database time zone [DBTIMEZONE] is relevant only for TIMESTAMP WITH LOCAL TIME ZONE columns. Oracle recommends that you set the database time zone to UTC (0:00)...
SYSDATE/SYSTIMESTAMP will return the time in the database server's OS timezone. Selecting a TIMESTAMP WITH LOCAL TIME ZONE datatype will return the time in your session's timezone (ie, SESSIONTIMEZONE).
select
CAST(systimestamp AS timestamp(0) with local time zone) as local_time,
systimestamp as server_time
from dual;
DBTIMEZONE is only used as the base timezone stored in TIMESTAMP WITH LOCAL TIME ZONE columns - which you never see, because when you select from one of those columns it gets translated into your session timezone.
See this similar question for a very detailed answer.
It is a common misunderstanding that SYSDATE or SYSTIMESTAMP are returned at DBTIMEZONE
SYSDATE and SYSTIMESTAMP are given in the time zone of database server's operating system. If you like to interrogate the time zone of database server's operating system run
SELECT TO_CHAR(SYSTIMESTAMP, 'tzr') FROM dual;
see also How to handle Day Light Saving in Oracle database

Why do oracle triggers show a different timestamp what is being inserted

My table has a Trigger that stores current_timestamp into a field when the row in the table is updated.
When a row is first inserted it is inserted with a Created date equal to Current_Timestamp. It appears when the procedure inserts the created_date it is given the correct time for daylight savings time but the trigger is inserting the incorrect time.
So the records in question are showing a created date of '2018-03-11 03:13:53.392086000' and a modified date of '2018-03-11 02:13:53.392086000'.
To add to this, we use goldengate for replication and it is using '2018-03-11 03:13:53.392086000' as the audit timestamp and not '2018-03-11 02:13:53.392086000'
Why would a trigger show a time differently then goldengate and the insert? database version I am working with is Oracle 11.2.0.4.0.
Trigger that handles the insert modified time
create or replace trigger UT_INSERT before insert on MYTABLE
REFERENCING NEW AS NEWROW OLD AS OLDROW FOR EACH ROW
begin
:NEWROW.MODIFIED_TIMESTAMP := CURRENT_TIMESTAMP;
end;
Trigger for updated modified time
create or replace trigger UT_UPDATE before update on MYTABLE
REFERENCING NEW AS NEWROW OLD AS OLDROW FOR EACH ROW
begin
:NEWROW.MODIFIED_TIMESTAMP := CURRENT_TIMESTAMP;
end;
The insert statement that is setting created_date is using Current_timestamp. All sessions are currently using a TZ offset of -05:00 and the sessiontimezone is America/Chicago.
That can't always be true. The only way to get the two values you have is for the insert to have been done with created_date set to systimestamp and for the session that issued that to have had it's session time zone set to -05:00, not to America/Chicago.
The reason that must be the case is that there was no 02:13 on March 11th because of the DST change:
select timestamp '2018-03-11 03:13:53.392086000 America/Chicago' from dual;
TIMESTAMP'2018-03-1103:13:53.392086000AMERICA
---------------------------------------------
2018-03-11 03:13:53.392086000 AMERICA/CHICAGO
select timestamp '2018-03-11 02:13:53.392086000 America/Chicago' from dual;
Error report -
SQL Error: ORA-01878: specified field not found in datetime or interval
01878. 00000 - "specified field not found in datetime or interval"
select timestamp '2018-03-11 02:13:53.392086000 -5:00' from dual;
TIMESTAMP'2018-03-1102:13:53.3920860
------------------------------------
2018-03-11 02:13:53.392086000 -05:00
So assuming the column value was set from your trigger, which seems entirely reasonable, the session which did that must have been -05:00 to be able to have got the timestamp at 02:13; and the created_date must have been using systimestamp, where the server OS had the correct time zone of America/Chicago to pick up the DST change (ignore dbtimezone for this), to have got 03:13 at the same moment in time.
As long as the session time zone and server time zone are the same you won't see a discrepancy between using current_timestamp or systimestamp, but you can't always control how sessions are configured, so a mistake or deliberate time zone change can cause problems like this.
Unless you are specifically trying to record a user's local time for some reason (and then you should probably be using a timestamp data type that preserves the time zone), it's safer to always use systimestamp for all of the values you record; and if you're storing as a plain timestamp then it might be safer to always store UTC time.
(Changing to UTC now, particularly with existing data to worry about, probably isn't an option; and not sure how Golden Gate would handle that but seems like a common scenario. Something to think about for the future though. Changing to always use systimestamp ought to be fairly transparent, except that you have this and probably other discrepancies to worry about. Finding and changing all code that might do inserts could be an issue - but then you seem to have inconsistencies already).

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.

Oracle 11g converts timestamp assuming time is Cental when it already is UTC

My company recently upgraded to Oracle 11g. I have a field - Timestamp with Time Zone that has data inserted in as "UTC". We query the data, make some changes and run a generic update statement that updates all the fields in the table. Since the 11g upgrade, the update statement converts the date/time assuming it's US/Central (my time zome). Example:
7/4/2015 7:40:04.000000 AM +00:00 turns into 7/4/2015 12:40:04.000000 AM +00:00 with an update statement like this:
Update Tablename
set FIELD_TIMESTAMP = to_timestamp('07/04/2015 7:40:04 AM', 'mm/dd/yyyy hh:mi:ss AM') at time zone 'UTC'
where ID = 201924;
I have tried just inserting a basic date, I have tried using different variations of the time zone. Nothing works, it always converts the timestamp.
How can I indicate to Oracle that the timestamp I'm updating is UTC and not US/Central time zone so it doesn't convert it?
Try using to_timestamp_tz. For example:
SQL> create table tz_test
(
id number,
col1 timestamp with time zone
)
Table created.
SQL> insert into tz_test values (1, systimestamp)
1 row created.
SQL> insert into tz_test values (2, to_timestamp('20150716 7:40:04', 'YYYYMMDD HH24:MI:SS'))
1 row created.
SQL> insert into tz_test values (3, to_timestamp_tz('20150716 7:40:04 -0:00', 'YYYYMMDD HH24:MI:SS TZH:TZM'))
1 row created.
SQL> commit
Commit complete.
SQL> select * from tz_test
ID COL1
---------- -----------------------------------
1 7/16/2015 1:29:47.031650 PM -04:00
2 7/16/2015 7:40:04.000000 AM -04:00
3 7/16/2015 7:40:04.000000 AM +00:00
3 rows selected.
My timezone is eastern (-4 from UTC), but the 3rd insert I specify the timezone to be used.
Reason behind this issue is that the TZ variable is not set properly in your.bash_profile.
As of now I am almost certain you database will be returning the systimestamp in central
SELECT SYSTIMESTAMP FROM DUAL;
In order to permanently fix (like you had prior to upgrade to Oracle 11g) you need to add or edit TZ variable to UTC. For instance if I want to change my timezone to Asia/Calcutta then I have to add/edit the TZ variable like mentioned below
TZ='Asia/Calcutta'; EXPORT TZ
Once you edit the .bash_profile you need to restart the listener service and database.
Or as an alternative you use can to_timestamp() and to_timestamp_tz() function as suggested by tbone to resolve this .
Regards
Andy

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