简体   繁体   中英

Oracle SQL: convert UTC to CST

I would like to convert UTC date/time to local CST.

The below function works however it gives 6 hours difference when there should only be 5 hours (until day light saving on 11/2/2014).

CAST((FROM_TZ(CAST(utc_date AS TIMESTAMP),'UTC') AT TIME ZONE 'CST') AS DATE) cst_date

also tried a variation

to_date(to_char((from_tz(to_timestamp(to_char(utc_date, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS') ,'UTC')
at time zone 'CST'),'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS') as cst_date,

Using the "US/Central" as the target timezone seems to produce the right result.

select from_tz(CAST ('15-oct-2014' AS TIMESTAMP),'GMT') at  TIME ZONE 'US/Central' with_daylight_savings,
       from_tz(CAST ('15-nov-2014' AS TIMESTAMP),'GMT') at  TIME ZONE 'US/Central' without_daylight_savings
from dual;


WITH_DAYLIGHT_SAVINGS                       WITHOUT_DAYLIGHT_SAVINGS
--------------------------------------------------------------------------------------
14-OCT-14 07.00.00.000000000 PM US/CENTRAL  14-NOV-14 06.00.00.000000000 PM US/CENTRAL

Use timezone region instead of timezone abbr ('CST'). You may find the desired timezone here:

SELECT * from v$timezone_names where tzabbrev = 'CST';

Maybe you need 'CST6CDT' instead of 'CST'

Maybe a stupid approach, but what do you get from this query?

SELECT 
   TO_CHAR((TIMESTAMP '2014-01-01 00:00:00' + LEVEL * INTERVAL '1' DAY) AT TIME ZONE 'America/Chicago', 'yyyy-mm-dd hh24:mi TZH:TZM') AS dst,
   TO_CHAR((FROM_TZ(CAST(DATE '2014-01-01' AS TIMESTAMP), 'UTC') + LEVEL * INTERVAL '1' DAY) AT TIME ZONE 'America/Chicago', 'yyyy-mm-dd hh24:mi TZH:TZM') AS dst
FROM dual
CONNECT BY LEVEL <= 365;

Is it as expected?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM