How to get null value from XML column.
XML requirements
XML tags are case sensitive. Opening and closing tags must be written with the same case
In XML, all elements must be properly nested within each other
In XML, the attribute values must always be quoted
“xsi:nil” represents the null value in XML
<message>salary < 1000</message> < < less than > > greater than & & ampersand ' ' apostrophe " " quotation mark
select name, object_id,principal_id from sys.tables for XML PATH, ELEMENTS XSINIL;
<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>spt_fallback_db</name> <object_id>117575457</object_id> <principal_id xsi:nil="true" /> </row> <row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>spt_fallback_dev</name> <object_id>133575514</object_id> <principal_id xsi:nil="true" /> </row> <row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>spt_fallback_usg</name> <object_id>149575571</object_id> <principal_id xsi:nil="true" /> </row> <row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>spt_monitor</name> <object_id>1803153469</object_id> <principal_id xsi:nil="true" /> </row> <row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>MSreplication_options</name> <object_id>2107154552</object_id> <principal_id xsi:nil="true" /> </row>
declare @xml xml select @xml = N'<row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>spt_fallback_db</name> <object_id>117575457</object_id> <principal_id xsi:nil="true" /> </row> <row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>spt_fallback_dev</name> <object_id>133575514</object_id> <principal_id xsi:nil="true" /> </row> <row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>spt_fallback_usg</name> <object_id>149575571</object_id> <principal_id xsi:nil="true" /> </row> <row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>spt_monitor</name> <object_id>1803153469</object_id> <principal_id xsi:nil="true" /> </row> <row xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <name>MSreplication_options</name> <object_id>2107154552</object_id> <principal_id xsi:nil="true" /> </row>' ;with xmlnamespaces('http://www.w3.org/2001/XMLSchema-instance' as xsi) select N.C.value('object_id[1]', 'int') as object_id ,N.C.value('principal_id[1][not(@xsi:nil = "true")]', 'int') as principal_id ,N.C.value('name[1]', 'varchar(100)') as name from @xml.nodes('//row') N(C)
Other Reference: