将XSD日期xs:dateTime转换为Oracle日期 [英] Converting a XSD date xs:dateTime to an Oracle Date

查看:122
本文介绍了将XSD日期xs:dateTime转换为Oracle日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从以下格式转换日期:

I need to convert a date from this format:

2002-10-10T12:00:00-05:00
(xs:dateTime定义在XML中)

2002-10-10T12:00:00-05:00 (xs:dateTime as defined in XML)

Oracle日期

我习惯于在PL / SQL中使用它:to_date('date here','yyyymmdd'),是否有一种方法可以在保留时区信息的同时转换?

I'm used to using this in PL/SQL: to_date('date here', 'yyyymmdd'), is there a way to convert this while keeping the time zone info?

谢谢

推荐答案

Oracle日期没有时区信息。您将需要使用TIMESTAMP数据类型。

Oracle dates don't have timezone information. You'll need to use a TIMESTAMP datatype instead.

它的工作原理如下:

SQL> desc tz
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER
 TS                                                 TIMESTAMP(6) WITH TIME ZONE
 TNOW                                               TIMESTAMP(6) WITH TIME ZONE

SQL> insert into tz
  2  values (1
  3          , to_timestamp_tz('2002-10-10 12:00:00-05:00'
  4                           , 'YYYY-MM-DD HH24:MI:SSTZH:TZM')
  5          , systimestamp)
  6  /

1 row created.

SQL> select * from tz
  2  /

        ID
----------
TS
---------------------------------------------------------------------------
TNOW
---------------------------------------------------------------------------
         1
10-OCT-02 12.00.00.000000 -05:00
23-AUG-10 17.37.06.502000 +01:00


SQL>

请注意,XSD符号中的 T 有棘手的问题。这导致了一个 ORA-01858 异常,因为它在Oracle中不是有效的格式。我确定有一个解决方法,但它目前正在逃脱我。

Note, there is the tricky issue of the T in the XSD notation. That hurls a ORA-01858 exception, because it's not a valid format in Oracle. I'm sure there is a workaround, but it currently escapes me.

嗯,一个解决办法是应用SUBSTR()函数sto split打开时间戳的两个部分,如Bob所示。但应该是一个更优雅的方式。

Well, one workaround is to apply SUBSTR() function sto split open the two parts of the timestamp, as Bob shows. But there ought to be a more elegant way.

它可能不符合优雅的限制,但由于它是一个字符串,我们可以使用替换函数来摆脱的烦人的T:

It probably doesn't qualify as "elegant" but as it's a string we can use a substitution function to get rid of the annoying T:

SQL> insert into tz
  2  values (2
  3          , to_timestamp_tz(translate('2003-10-10T12:00:00-05:00', 'T', ' ')
  4                   , 'YYYY-MM-DD HH24:MI:SSTZH:TZM')
  5          , systimestamp)
  6  /

1 row created.

SQL> select * from tz
  2  /

        ID
----------
TS
---------------------------------------------------------------------------
TNOW
---------------------------------------------------------------------------
         1
10-OCT-02 12.00.00.000000 -05:00
23-AUG-10 17.37.06.502000 +01:00

         2
10-OCT-03 12.00.00.000000 -05:00
23-AUG-10 17.53.37.113000 +01:00


SQL>

但是,由于Oracle将所有的努力放在XMLDB中,所以没有一个整理解决方案。

But given all the effort Oracle have put into XMLDB it is rather annoying that there isn't a tidier solution.


我不明白你如何得到
-05:00 。

"I dont understand how you get -05:00."

在我的原始示例中,我使用的格式掩码为'YYYY-MM-DD HH24:MI :SS-TZH:TZM'。这将解释时区中的 - 为分隔符,而不是减号。因此返回+05:00。我已经更正了我的代码示例,以删除那个最后一个破折号。现在时区正确呈现为-05:00。对不起任何混乱。

In my original sample I use a format mask of 'YYYY-MM-DD HH24:MI:SS-TZH:TZM'. This interprets the - in the time zone as a separator not a minus sign. Consequently it returned +05:00. I have since corrected my code sample to remove that last dash. Now the timezone is correctly rendered as -05:00. Sorry for any confusion.

这篇关于将XSD日期xs:dateTime转换为Oracle日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆