Time zone conversion in SQL query

折月煮酒 提交于 2019-11-30 23:48:40

问题


I am using a query to get some application Received Date from Oracle DB which is stored as GMT. Now I have to convert this to Eastern standard/daylight savings time while retrieving. I am using the below query for this:

   select to_char (new_time(application_recv_date,'gmt','est'), 'MON dd, YYYY') from application

It works fine for Standard time. But for daylight savings time we need to convert it to 'edt' based on timezone info. I am not very sure on how to do this. Please help me out


回答1:


You can use this query, without having to worry about timezone changes.

select to_char(cast(application_recv_date as timestamp) at time zone 'US/Eastern',
               'MON dd, YYYY'
              )
from application;

Ex:

EDT:

select cast(date'2014-04-08' as timestamp) d1,
       cast(date'2014-04-08' as timestamp) at time zone 'US/Eastern' d2
from dual;

D1                                 D2
---------------------------------- -------------------------------------------
08-APR-14 12.00.00.000000 AM       07-APR-14 08.00.00.000000 PM US/EASTERN

EST:

select cast(date'2014-12-08' as timestamp) d1,
       cast(date'2014-12-08' as timestamp) at time zone 'US/Eastern' d2
from dual;

D1                                 D2
---------------------------------- -------------------------------------------
08-DEC-14 12.00.00.000000 AM       07-DEC-14 07.00.00.000000 PM US/EASTERN

UPDATE:

Thanks to Alex Poole for reminding that, when timezone is not specified, local timezone is used for conversion.

To force the date to be recognized as GMT, use from_tz.

from_tz(cast(date'2014-12-08' as timestamp), 'GMT') at time zone 'US/Eastern'



回答2:


In Oracle, you can achieve this with below query:

Select current_timestamp, current_timestamp at time zone 'Australia/Sydney' from dual;

Where Australia/Sydney is the name of your timezone in which you want to convert your time.



来源:https://stackoverflow.com/questions/22933608/time-zone-conversion-in-sql-query

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