SQL、OPENXML 和 DateTime 对话

发布于 2024-08-02 02:41:38 字数 766 浏览 10 评论 0原文

我继承了大量存储过程,它们将 XML 块作为唯一参数,然后作者破解它(使用 OPENXML),然后更新一行。

我在更新具有日期时间列的表时遇到问题,以下是当前失败的数据片段:

declare @stuff nvarchar (max);

set @stuff =
'<Booking xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  <UpdatedDateTime>2009-08-14T15:32:09.7240556+01:00</UpdatedDateTime>
</Booking>';

declare @iDoc int;

exec sp_xml_preparedocument @idoc OUTPUT, @stuff;

SELECT UpdatedDateTime
    FROM 
        OPENXML(@idoc, '/*')
    WITH
    (
        UpdatedDateTime datetime 'UpdatedDateTime'
    )

运行会导致以下错误:

消息 241,级别 16,状态 1,第 12 行

转换时转换失败 来自字符串的日期时间。

如何将 XML 片段中的日期时间转换为基于 SQL 的日期时间?

谢谢

I've inherited a mass of stored procedures that take as their only parameter a block of XML, the author then cracks it (using OPENXML) and then updates the a row.

I've come across a problem while updating a table that has datetime columns, the following is a snippet of data that currently fails:

declare @stuff nvarchar (max);

set @stuff =
'<Booking xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  <UpdatedDateTime>2009-08-14T15:32:09.7240556+01:00</UpdatedDateTime>
</Booking>';

declare @iDoc int;

exec sp_xml_preparedocument @idoc OUTPUT, @stuff;

SELECT UpdatedDateTime
    FROM 
        OPENXML(@idoc, '/*')
    WITH
    (
        UpdatedDateTime datetime 'UpdatedDateTime'
    )

Running that causes the following error:

Msg 241, Level 16, State 1, Line 12

Conversion failed when converting
datetime from character string.

How do I go about converting the datetime from the XML fragment into a SQL based date time?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2024-08-09 02:41:38

尝试:

declare @stuff nvarchar (max);

    set @stuff =
    '<Booking xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
      <UpdatedDateTime>2009-08-14T15:32:09.7240556+01:00</UpdatedDateTime>
    </Booking>';

    declare @iDoc int;

    exec sp_xml_preparedocument @idoc OUTPUT, @stuff;

    SELECT CONVERT(datetime,REPLACE(LEFT(UpdatedDateTime,23),'T',' ')),RIGHT(UpdatedDateTime,6)
        ,CASE
             WHEN LEFT(RIGHT(UpdatedDateTime,6),1)='+' THEN DATEADD(hh,CONVERT(int,LEFT(RIGHT(UpdatedDateTime,5),2)),CONVERT(datetime,REPLACE(LEFT(UpdatedDateTime,23),'T',' ')))
             ELSE DATEADD(hh,-1*CONVERT(int,LEFT(RIGHT(UpdatedDateTime,5),2)),CONVERT(datetime,REPLACE(LEFT(UpdatedDateTime,23),'T',' ')))
         END

        FROM 
            OPENXML(@idoc, '/*')
        WITH
        (
            UpdatedDateTime varchar(100) 'UpdatedDateTime'
        )

结果集:

----------------------- ------ -----------------------
2009-08-14 15:32:09.723 +01:00 2009-08-14 16:32:09.723

(1 row(s) affected)

try:

declare @stuff nvarchar (max);

    set @stuff =
    '<Booking xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
      <UpdatedDateTime>2009-08-14T15:32:09.7240556+01:00</UpdatedDateTime>
    </Booking>';

    declare @iDoc int;

    exec sp_xml_preparedocument @idoc OUTPUT, @stuff;

    SELECT CONVERT(datetime,REPLACE(LEFT(UpdatedDateTime,23),'T',' ')),RIGHT(UpdatedDateTime,6)
        ,CASE
             WHEN LEFT(RIGHT(UpdatedDateTime,6),1)='+' THEN DATEADD(hh,CONVERT(int,LEFT(RIGHT(UpdatedDateTime,5),2)),CONVERT(datetime,REPLACE(LEFT(UpdatedDateTime,23),'T',' ')))
             ELSE DATEADD(hh,-1*CONVERT(int,LEFT(RIGHT(UpdatedDateTime,5),2)),CONVERT(datetime,REPLACE(LEFT(UpdatedDateTime,23),'T',' ')))
         END

        FROM 
            OPENXML(@idoc, '/*')
        WITH
        (
            UpdatedDateTime varchar(100) 'UpdatedDateTime'
        )

results set:

----------------------- ------ -----------------------
2009-08-14 15:32:09.723 +01:00 2009-08-14 16:32:09.723

(1 row(s) affected)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文