Oracle SQL: converting timestamp to UTC

Fearghal picture Fearghal · Mar 27, 2014 · Viewed 79.4k times · Source

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

Answer

a_horse_with_no_name picture a_horse_with_no_name · Mar 27, 2014
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.