How to get UTC value for SYSDATE on Oracle

2019-01-17 13:58发布

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).

For now I've custom function :(

Cheers,

Stefan

5条回答
成全新的幸福
2楼-- · 2019-01-17 14:44

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

查看更多
倾城 Initia
3楼-- · 2019-01-17 14:45

I'm using:

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

It's working fine for me.

查看更多
再贱就再见
4楼-- · 2019-01-17 14:52
select sys_extract_utc(systimestamp) from dual;

Won't work on Oracle 8, though.

查看更多
Animai°情兽
5楼-- · 2019-01-17 14:56

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;
查看更多
狗以群分
6楼-- · 2019-01-17 14:57

Usually, I work with DATE columns, not the larger but more precise TIMESTAMP.

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

CAST(sys_extract_utc(SYSTIMESTAMP) AS DATE)

Working with UTC dates is great because I don't have to worry about the complexity of time zones. Only the final display to the user needs offsets or other trouble.

查看更多
登录 后发表回答