How to get UTC value for SYSDATE on Oracle

后端 未结 5 1121
耶瑟儿~
耶瑟儿~ 2020-12-05 04:03

Probably a classic... Would you know a easy trick to retrieve an UTC value of SYSDATE on Oracle (best would be getting something working on the 8th version as well).

相关标签:
5条回答
  • 2020-12-05 04:29

    You can use

    SELECT SYS_EXTRACT_UTC(TIMESTAMP '2000-03-28 11:30:00.00 -02:00') FROM DUAL;
    

    You may also need to change your timezone

    ALTER SESSION SET TIME_ZONE = 'Europe/Berlin';
    

    Or read it

    SELECT SESSIONTIMEZONE, CURRENT_TIMESTAMP FROM dual;
    
    0 讨论(0)
  • 2020-12-05 04:36

    Usually, I work with DATE columns, not the larger but more precise TIMESTAMP used by some answers.

    The following will return the current UTC date as just that -- a DATE.

    CAST(sys_extract_utc(SYSTIMESTAMP) AS DATE)
    

    I often store dates like this, usually with the field name ending in _UTC to make it clear for the developer. This allows me to avoid the complexity of time zones until last-minute conversion by the user's client. Oracle can store time zone detail with some data types, but those types require more table space than DATE, and knowledge of the original time zone is not always required.

    0 讨论(0)
  • 2020-12-05 04:37

    I'm using:

    SELECT CAST(SYSTIMESTAMP AT TIME ZONE 'UTC' AS DATE) FROM DUAL;
    

    It's working fine for me.

    0 讨论(0)
  • 2020-12-05 04:37

    If you want a timestamp instead of just a date with sysdate, you can specify a timezone using systimestamp:

    select systimestamp at time zone 'UTC' from dual
    

    outputs: 29-AUG-17 06.51.14.781998000 PM UTC

    0 讨论(0)
  • 2020-12-05 04:41
    select sys_extract_utc(systimestamp) from dual;
    

    Won't work on Oracle 8, though.

    0 讨论(0)
提交回复
热议问题