如何在 Oracle 上获取 SYSDATE 的 UTC 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1157873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 02:01:44  来源:igfitidea点击:

How to get UTC value for SYSDATE on Oracle

oracletimezoneutc

提问by stic

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

可能是经典之作……您知道在 Oracle 上检索 SYSDATE UTC 值的简单技巧吗(最好也能在第 8 个版本上运行一些东西)。

For now I've custom function :(

现在我有自定义功能:(

Cheers,

干杯,

Stefan

斯特凡

采纳答案by Jonathan

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;

回答by Juris

select sys_extract_utc(systimestamp) from dual;

Won't work on Oracle 8, though.

但是不适用于 Oracle 8。

回答by Charles Burns

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

通常,我使用 DATE 列,而不是更大但更精确的 TIMESTAMP。

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

以下将返回当前的 UTC 日期——一个 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.

使用 UTC 日期非常棒,因为我不必担心时区的复杂性。只有最终显示给用户需要偏移或其他麻烦。

回答by Pedro Sobral

I'm using:

我正在使用:

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

It's working fine for me.

它对我来说很好用。

回答by Nathan Strutz

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

如果你想要一个时间戳而不仅仅是一个带有 sysdate 的日期,你可以使用 systimestamp 指定一个时区:

select systimestamp at time zone 'UTC' from dual

outputs: 29-AUG-17 06.51.14.781998000 PM UTC

输出: 29-AUG-17 06.51.14.781998000 PM UTC