Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL: converting timestamp to UTC

I have a simple select query such as below but I noticed I am getting back the regional times. How can I convert to UTC in my select statment?

select myTimeStamp, MyName, MyBranch from tableA

Result: '27/03/2014 15:15:26' 'john', 'london'

I have tried using sys_extract_utc (myTimeStamp) but I have the error

sql command not properly ended

The column myTimestamp is of type 'date'.

like image 296
Fearghal Avatar asked Mar 27 '14 13:03

Fearghal


People also ask

Does Oracle store TIMESTAMP in UTC?

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.

Does Oracle TIMESTAMP have timezone?

Oracle converts the data to a TIMESTAMP WITH LOCAL TIME ZONE value. This means the time zone that is entered ( -08:00 ) is converted to the session time zone value ( -07:00 ).

What is the format of TIMESTAMP in Oracle?

Oracle introduced TIMESTAMP data type in 9i version. It allows to store date time with fraction of seconds. By default format of TIMESTAMP is 'YYYY-MM-DD HH24:MI:SS. FF'.


2 Answers

select cast(mytimestamp as timestamp) at time zone 'UTC', 
       MyName, 
       MyBranch 
from tableA

Because mytimestamp is in fact a date not a timestamp you need to cast it. Doing that makes Oracle assume that the information stored in mytimestamp is in the time zone of the server -if that isn't the case you need to use Madhawas' solution.

like image 136
a_horse_with_no_name Avatar answered Sep 18 '22 11:09

a_horse_with_no_name


Depending on the type, there are a couple of gotchas with regard to what time zone Oracle is converting from depending on what the data type of myTimestamp is.

timestamp with time zone

It Just Works™. a_horse_with_no_name has the right answer here.

timestamp with local time zone

it is implicitly cast to timestamp with time zone, then It Just Works™. Again, a_horse_with_no_name is right here.

timestamp

While it too is implicitly cast to timestamp with time zone, the time zone that gets assigned by default is the session time zone (as opposed to the database time zone).

  • The explicit invocation of this is myTimestamp at local.
  • Alternatively (and most likely better), you can do as Madhawas says and use the from_tz function to explicitly build a value with an explicit time zone other than that of your session.

date

Trying to do any of the above to date will fail as you described:

  • myTimestamp at time zone 'UTC'
    ORA-30084: invalid data type for datetime primary with time zone modifier

  • from_tz(myTimestamp, 'America/New_York')
    ORA-00932: inconsistent datatypes: expected TIMESTAMP got DATE

The solution here is to cast the date to a timestamp first:

select from_tz(cast(myTimestamp as timestamp), 'America/New_York') from tableA

Sample Script

The following script illustrates the behavior. Note that on my system, dbtimezone is US/Central, and sessiontimezone is GMT-05:00.

I also use to_char to convert the output as I have found some tools will alter the result timestamp in subtle ways, particularly if they don't have good timestamp support (this is rare nowadays, but still potentially a problem).

alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'
/
alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS'
/
alter session set nls_timestamp_tz_format = 'YYYY-MM-DD HH24:MI:SS TZR'
/

select dbtimezone
      ,sessiontimezone
      ,to_char(timestamp '2017-01-01 06:00:00') as ts
      ,to_char(timestamp '2017-01-01 06:00:00' at local) as ts_at_local
      ,to_char(timestamp '2017-01-01 06:00:00' at time zone dbtimezone) as ts_at_db
      ,to_char(timestamp '2017-01-01 06:00:00' at time zone sessiontimezone) as ts_at_session
 from dual
/

The output on my system is as follows (reformatted as columnar for readability):

DBTIMEZONE          US/Central
SESSIONTIMEZONE     -05:00
TS                  2017-01-01 06:00:00
TS_AT_LOCAL         2017-01-01 06:00:00 -05:00
TS_AT_DB            2017-01-01 05:00:00 US/CENTRAL
TS_AT_SESSION       2017-01-01 06:00:00 -05:00
like image 43
Chris R. Donnelly Avatar answered Sep 19 '22 11:09

Chris R. Donnelly