Selecting null value from XML in SQL Server

前端 未结 8 1524
Happy的楠姐
Happy的楠姐 2021-02-19 20:23

I\'m trying to select from XML that has a null as one of the attributes. Instead of returning a null, it returns a 0. What am I doing wrong?
See code below to replicate:

8条回答
  •  遥遥无期
    2021-02-19 21:01

    http://go4answers.webhost4life.com/Example/including-null-columns-empty-elements-125474.aspx

    [not(@xsi:nil = "true")]
    

    This will select null. By the way author code has a typo

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instace"
    

    instance is misspelled as instace

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    

    Working version of author code

    declare @a xml
                select @a = '
                  
                    1
                    1
                  
                  
                    
                    2
                  
                  
                    3
                    3
                  
                '
    
                 select ParamValues.TaskChainerTask.value('./Property1[1][not(@xsi:nil = "true")]','int') as Property1,
                        ParamValues.TaskChainerTask.value('./Property2[1][not(@xsi:nil = "true")]','int') as Property2
                   from @a.nodes('(/TestSet/Element)') as ParamValues(TaskChainerTask)
    

提交回复
热议问题