Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle how to convert time in UTC to the local time (offset information is missing)

Tags:

sql

oracle

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?

like image 914
Max_Salah Avatar asked Jul 13 '16 08:07

Max_Salah


People also ask

How do I set UTC time zone in Oracle?

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';

How do I convert UTC time to local time in SQL?

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.

Does Oracle store TIMESTAMP in UTC?

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).

How do I change timezone in Oracle query?

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.


1 Answers

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
like image 91
MT0 Avatar answered Oct 01 '22 08:10

MT0