Selecting null value from XML in SQL Server

前端 未结 8 1517
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 20:42

    I would sugest this approach:

    DECLARE @a XML = '
      
        1
        1
      
      
        
        2
      
      
        3
        3
      
    '
    
    SELECT
        ParamValues.TaskChainerTask
            .value('./Property1[not(./@*[local-name()="nil"] = "true")][1]', 'int') as Property1,
        ParamValues.TaskChainerTask
            .value('./Property2[not(./@*[local-name()="nil"] = "true")][1]', 'int') as Property2
    FROM @a.nodes('//Element') ParamValues(TaskChainerTask)
    

提交回复
热议问题