I have a table which contains a date column. I think that the date in that column is saved in UTC.
I would like when the date is retrieved that it is printed in local time. That means when I call the date from Germany, the result should be something like this:
2015-04-29 11:24:06 +0200UTC EUROPE/BERLIN
I tried the following sql:
SELECT TO_CHAR(CAST(dateColum as TIMESTAMP WITH LOCAL TIME ZONE), 'YYYY-MM-DD HH24:MI:SS TZR') from myTable;
the result looks like this:
2015-04-29 11:24:06 EUROPE/BERLIN
+/- offset is missing.
Any idea?
Use the ALTER DATABASE SET TIME_ZONE command to change the time zone of a database. This command takes either a named region such as America/Los_Angeles or an absolute offset from UTC. This example sets the time zone to UTC: ALTER DATABASE SET TIME_ZONE = '+00:00';
SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(DATETIMEOFFSET, GETUTCDATE()), DATENAME(TZOFFSET, SYSDATETIMEOFFSET()))) AS LOCAL_IST; Here, the GETUTCDATE() function can be used to get the current date and time UTC. Using this query the UTC gets converted to local IST.
Other time zones & offsets This documentation for Oracle Database seems to say that TIMESTAMP WITH TIME ZONE type does record the incoming data's time zone or offset-from-UTC. Some other databases such as Postgres adjust incoming values to UTC (an offset of zero hours-minutes-seconds).
The time_zone is normally set at database creation time: SQL> create database . . . set time_zone='+00:00'; To change the Oracle time zone for an existing Oracle database, we change the time_zone parameter and then bounce the database.
Oracle Setup:
CREATE TABLE table_name ( value ) AS
SELECT DATE '2016-07-13' FROM DUAL;
Query:
SELECT TO_CHAR(
FROM_TZ( CAST( value AS TIMESTAMP ), 'UTC' )
AT TIME ZONE 'EUROPE/BERLIN',
'YYYY-MM-DD HH24:MI:SS TZH:TZM TZR'
) AS berlin_time
FROM table_name;
Output:
BERLIN_TIME
----------------------------------------
2016-07-13 02:00:00 +02:00 EUROPE/BERLIN
Query 2:
SELECT TO_CHAR(
FROM_TZ( CAST( value AS TIMESTAMP ), 'UTC' ) AT LOCAL,
'YYYY-MM-DD HH24:MI:SS TZH:TZM TZR'
) AS local_time
FROM table_name;
Output:
LOCAL_TIME
----------------------------------------
2016-07-13 02:00:00 +02:00 EUROPE/BERLIN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With