Selecting null value from XML in SQL Server

前端 未结 8 1471
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条回答
  •  -上瘾入骨i
    2021-02-19 20:48

    I'm not sure if your particular case requires you to sub-query the nodes first, but if not you can request the .value and provide the xPath. Since the Property1 node exists, you want to evaluate the text() of the Property1 node and not the node itself:

     select  ParamValues.TaskChainerTask.value('Property1[1]/text()[1]','int') as Property1,
            ParamValues.TaskChainerTask.value('Property2[1]/text()[1]','int') as Property2
    
       from @a.nodes('(/TestSet/Element)') as ParamValues(TaskChainerTask)
    

    In addition to make sure this works in other cases you can provide the most detailed element path in the @a.nodes xPath statment and walk up with "../" instead of sub-querying the node results.

提交回复
热议问题