How do I convert an oracle timestamp value from UTC to EST in a select statement?

自古美人都是妖i 提交于 2021-02-08 10:41:21

问题


Hi I have a date field (Open_Time) containing timestamp data. The times in this column are in UTC. I want to select that column and convert it to EST and insert it into another table. I want it to be EST for the time at the original timestamp (take into account daylight savings time based on what day month and year it was). I have been reading about the various timezone functions in oracle but most seem to focus on altering the database's timezone which I don't need to do. The (Open_Time) field is always recorded in UTC.

BLUF: I need to select a time_stamp field (Open_Time) that was recorded according to UTC time and convert it to what EST was at the time of (Open_Time). Thanks.


回答1:


If you are using the TIMESTAMP WITH TIME ZONE data type:

Oracle Setup:

CREATE TABLE Table_Name (
  open_time TIMESTAMP WITH TIME ZONE
);

INSERT INTO Table_Name VALUES ( SYSTIMESTAMP AT TIME ZONE 'UTC' );

Query:

SELECT open_time AT TIME ZONE 'UTC' AS utc,
       open_time AT TIME ZONE 'EST' AS est
FROM   Table_Name;

Output:

UTC                                 EST                               
----------------------------------- -----------------------------------
02-MAR-16 22.41.38.344809000 UTC    02-MAR-16 17.41.38.344809000 EST    

or if you are just using the TIMESTAMP data type:

Oracle Setup:

CREATE TABLE Table_Name (
  open_time TIMESTAMP
);

INSERT INTO Table_Name VALUES ( SYSTIMESTAMP );

Query:

SELECT CAST( open_time AS TIMESTAMP WITH TIME ZONE ) AT TIME ZONE 'UTC' AS utc,
       CAST( open_time AS TIMESTAMP WITH TIME ZONE ) AT TIME ZONE 'EST' AS est
FROM   Table_Name;

Output:

UTC                                 EST                               
----------------------------------- -----------------------------------
02-MAR-16 22.41.38.344809000 UTC    02-MAR-16 17.41.38.344809000 EST    


来源:https://stackoverflow.com/questions/35759390/how-do-i-convert-an-oracle-timestamp-value-from-utc-to-est-in-a-select-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!