Check and set the database and session time zone in Oracle

Setting database and session time zone in Oracle

Oracle choose default time zone as operating system during installation of Oracle. If you want to set the database time zone at create time then use SET TIME_ZONE with CREATE DATABASE statement.

Set the time zone at database level

-- Check the time zone
SELECT dbtimezone FROM DUAL;
DBTIME
------
+00:00

--Change the time zone of database
ALTER DATABASE SET TIME_ZONE='Europe/London';
OR
ALTER DATABASE SET TIME_ZONE='-05:00';

--restart the database
Shutdown immediate;
startup

-- Check the time zone
SELECT dbtimezone FROM DUAL;
DBTIME
------
-05:00

Set the time zone at session level

-- use alter session commands
ALTER SESSION SET TIME_ZONE=local;
ALTER SESSION SET TIME_ZONE=dbtimezone;
ALTER SESSION SET TIME_ZONE='Asia/Hong_Kong';
ALTER SESSION SET TIME_ZONE='+10:00';

SQL> SELECT sessiontimezone FROM DUAL;
SESSIONTIMEZONE
---------------
+05:30

SQL> alter session set time_zone='+10:00';
Session altered.

SQL> SELECT sessiontimezone FROM DUAL;
SESSIONTIMEZONE
---------------
+10:00

Convert timezone from one to another

SELECT FROM_TZ(CAST(TO_DATE('2018-11-02 03:00:00','YYYY-MM-DD HH:MI:SS') AS TIMESTAMP), '+5:30') AT TIME ZONE '-6:00' "West Coast Time" FROM DUAL;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.