如何在XML Select查询中处理MIN日期

时间:2014-05-11 05:37:22

标签: sql xml sql-server-2008 sqlxml

我有XML Type变量,它保存XML数据,我想从XML检索数据,一切正常,但是当date字段的MIN日期值(0001-01-01T00:00:00)比给出错误时。有没有办法消除这个错误?

错误消息

The conversion of a datetimeoffset data type to a datetime data type resulted in an out-of-range value.

--Giving Error
DECLARE @tmpXML XML
SET @tmpXML ='<FormSurveyorNoteXML><FormSurveyorNoteInfo><SurveyDate>2014-05-11T00:00:00+05:30</SurveyDate><InformationDate>0001-01-01T00:00:00</InformationDate></FormSurveyorNoteInfo></FormSurveyorNoteXML>'

SELECT  
                        T1.TR1.value('InformationDate [1]', 'datetime'),
                        T1.TR1.value('SurveyDate [1]', 'datetime')                      
FROM   @tmpXML.nodes('/FormSurveyorNoteXML/FormSurveyorNoteInfo') AS T1(TR1)

--Working Perfect, when date is not MIN
DECLARE @tmpXML1 XML
SET @tmpXML1 ='<FormSurveyorNoteXML><FormSurveyorNoteInfo><SurveyDate>2014-05-11T00:00:00+05:30</SurveyDate><InformationDate>1753-01-01T00:00:00</InformationDate></FormSurveyorNoteInfo></FormSurveyorNoteXML>'

SELECT  
                        T1.TR1.value('InformationDate [1]', 'datetime'),
                        T1.TR1.value('SurveyDate [1]', 'datetime')                      
FROM   @tmpXML1.nodes('/FormSurveyorNoteXML/FormSurveyorNoteInfo') AS T1(TR1)

如果您有任何建议或输入来处理XML Select中的MIN日期问题,请告诉我。

由于 苏雷什

1 个答案:

答案 0 :(得分:2)

回答我自己的问题,我发现解决方案必须使用datetime2而不是datetime,因为datetime2的日期范围更大。

SELECT  
                        T1.TR1.value('InformationDate [1]', 'datetime2'),
                        T1.TR1.value('SurveyDate [1]', 'datetime')                      
FROM   @tmpXML.nodes('/FormSurveyorNoteXML/FormSurveyorNoteInfo') AS T1(TR1)