Oracle 日期格式“2009-02-13T11:46:40+00:00”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/825121/
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-18 18:10:03  来源:igfitidea点击:

Oracle Date formatting "2009-02-13T11:46:40+00:00"

oracledateformatting

提问by Gareth

I've had some brilliant help before and I'm hoping you can get me out of a hole again.

我以前得到过一些出色的帮助,我希望你能再次让我摆脱困境。

I've got a date coming in from a web service in this format: 2009-02-13T11:46:40+00:00

我有一个来自网络服务的日期,格式如下:2009-02-13T11:46:40+00:00

which to me looks like standard UTC format.

对我来说,它看起来像标准的 UTC 格式。

I need to insert it into an Oracle database, so I'm using to_date() on the insert. Problem is, I cant get a matching formatting string for it and keep getting "ORA-01861: literal does not match format string" errors.

我需要将它插入到 Oracle 数据库中,所以我在插入时使用了 to_date()。问题是,我无法为它获取匹配的格式字符串并不断收到“ORA-01861:文字与格式字符串不匹配”错误。

I know its a fairly trivial problem but for some reason I cannot get it to accept the right format string. Any help appreciated.

我知道这是一个相当微不足道的问题,但由于某种原因,我无法让它接受正确的格式字符串。任何帮助表示赞赏。

Thanks :)

谢谢 :)

Gareth

加雷斯

回答by Dave Costa

You can directly convert it to a TIMESTAMP_WITH_TIME_ZONE datatype.

您可以直接将其转换为 TIMESTAMP_WITH_TIME_ZONE 数据类型。

select
  to_timestamp_tz('2009-02-13T11:46:40+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
from
 dual

TO_TIMESTAMP_TZ('2009-02-13T11:46:40+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM
---------------------------------------------------------------------------
13-FEB-09 11.46.40.000000000 AM +00:00

(I'm assuming the input string is using a 24-hour clock since there is no AM/PM indicator.)

(我假设输入字符串使用 24 小时制,因为没有 AM/PM 指示器。)

If you want to convert that to a simple DATE, you can, but it will lose the time zone information.

如果您想将其转换为简单的 DATE,您可以,但它会丢失时区信息。

回答by Quassnoi

SELECT  CAST(TO_TIMESTAMP_TZ(REPLACE('2009-02-13T11:46:40+00:00', 'T', ''), 'YYYY-MM-DD HH:MI:SS TZH:TZM') AS DATE)
FROM    dual

回答by Micha? Niklas

To import date in specified format you can set nls_date_format.

要以指定格式导入日期,您可以设置nls_date_format.

Example:

例子:

alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS'

This way your SQL statements can be shorter (no casts). For various mask look at Datetime Format Models

这样你的 SQL 语句可以更短(没有强制转换)。对于各种掩码,请查看日期时间格式模型