如何将XML字符串中的空值插入数据库。
当我插入时,然后DOB字段插入'1900-01-01 00:00:00.000'和性别字段''。 我想向此列插入NULL值。请帮我。
<Child>
<EnrolmentNo>01113702010</EnrolmentNo>
<Name>Ankush</Name>
<DOB></DOB>
<Email>Anushka@gmail.com</Email>
<Gender></Gender>
<Phone>9990234323, 9452344321</Phone>
<RegistrationDate>2013-11-12T00:00:00</RegistrationDate>
<ValidUpto>2014-12-12T00:00:00</ValidUpto>
</Child>
@Input XML
INSERT INTO KP_StudentsTemp
(
[EnrolmentNo],[SessionId],[BatchId],[SemesterId],
[FirstName],[MiddleName],[LastName],[DOB],[Email],[Gender],[Phone],
[RegistrationDate],[ValidUpto]
)
SELECT (XNodes.value('(EnrolmentNo)[1]','varchar(32)')),
XNodes.value('(Name)[1]','varchar(32)'),
XNodes.value('(DOB)[1]','datetime'),
XNodes.value('(Email)[1]','varchar(256)'),
XNodes.value('(Gender)[1]','char(1)'),
XNodes.value('(Phone)[1]','varchar(64)'),
XNodes.value('(RegistrationDate)[1]','datetime'),
XNodes.value('(ValidUpto)[1]','datetime')
FROM @input.nodes('/Main/Child') AS XTbl(XNodes)
答案 0 :(得分:1)
您可以使用NULLIF
功能
@Input XML
INSERT INTO KP_StudentsTemp
(
[EnrolmentNo],[SessionId],[BatchId],[SemesterId],
[FirstName],[MiddleName],[LastName],[DOB],[Email],[Gender],[Phone],
[RegistrationDate],[ValidUpto]
)
SELECT (
XNodes.value('(EnrolmentNo)[1]','varchar(32)')),
XNodes.value('(Name)[1]','varchar(32)'),
NULLIF(XNodes.value('(DOB)[1]','datetime'),'1900-01-01 00:00:00.000'),
XNodes.value('(Email)[1]','varchar(256)'),
NULLIF(XNodes.value('(Gender)[1]','char(1)'),''),
XNodes.value('(Phone)[1]','varchar(64)'),
XNodes.value('(RegistrationDate)[1]','datetime'),
XNodes.value('(ValidUpto)[1]','datetime')
FROM @input.nodes('/Main/Child') AS XTbl(XNodes)