简体   繁体   中英

How to get UTC date/time from a date/time string that contains timezone offset in Oracle

Given a string like this: '2015-11-23T07:00:10.563-04:00' I am trying to determine the UTC date/time which I would expect to be: '2015-11-23 11:00:10.5630000'

I have tried the following:

SELECT sys_extract_utc(
        to_timestamp_tz('2015-11-23T07:00:10.563-04:00', 
                        'yyyy-mm-dd"T"hh24:mi:ss.FF TZH:TZM')
         )  
FROM DUAL;

but this produces the result:

2015/11/23 03:00:10.563000000

Even when I change the timezone offset to positive, I get the same result. I must be misunderstanding something so would appreciate your help.

There is no space between fractional seconds and timezone hour. So remove space from you pattern, and it'll give desired result.

SQL> SELECT sys_extract_utc(to_timestamp_tz('2015-11-23T07:00:10.563-04:00', 'yyyy-mm-dd"T"hh24:mi:ss.FFTZH:TZM'))  
FROM DUAL; 

SYS_EXTRACT_UTC(TO_TIMESTAMP_TZ('2015-11-23T07:00:10.563-04:00','YYYY-MM-DD
---------------------------------------------------------------------------
23-NOV-15 11.00.10.563000000 AM

Fixed it - the space character between ".....hh24:mi:ss.FF TZH:TZM" was causing the problem.

This now works perfectly:

SELECT sys_extract_utc(to_timestamp_tz('2015-11-23T07:00:10.563+04:00', 'yyyy-mm-dd"T"hh24:mi:ss.FFTZH:TZM'))  FROM DUAL;

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