I have an application that stores its timestamps in Oracle using UTC (Coordinated Universal Time). Another application reads these timestamps from the database, but there is no convenient way to convert the timestamps to another timezone automatically (from within that application). Is it possible to adjust session settings in Oracle to return timezone adjusted timestamps for SELECT queries?
Best Answer
Since the data is stored in a
TIMESTAMP
column, you cannot automatically convert it. You'll have to write some code that tells Oracle what time zone to treat the data as being stored in theGMT
time zone and tell it that you want it converted to the session's time zone. You would get the automatic behavior you are looking for if you used theTIMESTAMP WITH LOCAL TIME ZONE
data type instead. If you used theTIMESTAMP WITH TIME ZONE
data type, you would avoid having to tell the database what time zone the timestamp comes from when you write the conversion.Assuming that your session sets its time zone properly
for example, would tell Oracle that the current session is 8 hours before GMT (currently the Pacific time zone), if the data is stored in a
TIMESTAMP
column, you'd need something likeIf the data is stored in a
TIMESTAMP WITH TIME ZONE
column, you'd just needIf the data is stored in a
TIMESTAMP WITH LOCAL TIME ZONE
column, you would just select that column.An example
In response to the comment below from DylanKlomparens -- you cannot just apply the
AT TIME ZONE
to aTIMESTAMP
and expect to get the proper result so I'm not sure that I understand what you are seeing. If you just to anAT TIME ZONE
on a plainTIMESTAMP
, the timestamp is simply treated as having the time zone that you specified. It doesn't change the time. It also doesn't depend on the session time zone. In all these cases, ifts
represents aUTC
time, thePST
version represents the wrong time-- there should be an 8 hour difference between the starting and ending values.In response to Vincent's comment-- A
TIMESTAMP WITH LOCAL TIME ZONE
does adjust automatically to the session's time zone regardless of the server's time zone. As I adjust my session's time zone, the results change as well.