如何反序列化在T-SQL中使用FOR XML AUTO序列化的数据记录?

时间:2011-05-27 10:26:53

标签: sql sql-server xml tsql serialization

在我的Microsoft SQL Server 2005数据库中,我有WorkingDays列中的数据,如:

<WorkingDays>
  <Day>2011-05-05</Day>
  <Day>2011-05-06</Day>
</WorkingDays>

如何在T-SQL查询中将它们反序列化为DateTime格式?

enter image description here

1 个答案:

答案 0 :(得分:4)

尝试这样的事情:

DECLARE @TEST TABLE(ID INT IDENTITY, XmlContent XML)

INSERT INTO @TEST(XmlContent) VALUES('<WorkingDays>
  <Day>2011-05-05</Day>
  <Day>2011-05-06</Day>
</WorkingDays>')


SELECT 
    ID,
    WD.DAYS.value('(.)[1]', 'datetime') AS 'Day'
FROM 
    @TEST
CROSS APPLY 
    XmlContent.nodes('/WorkingDays/Day') AS WD(Days)

这给了我一个输出:

ID    Day
1     2011-05-05 00:00:00.000
1     2011-05-06 00:00:00.000