0

I have some xmls with different structures from which I want to get all the values including the nulls. After some time I got to write this little sample piece of code:

declare @xml xml = '
<e xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Code>code1</Code>
  <DepartmentCode xsi:nil="true" />
  <Email>email1</Email>
  <AddressId xsi:nil="true" />
  <IsActive>1</IsActive>
</e>
<e xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Code>code2</Code>
  <DepartmentCode xsi:nil="true" />
  <Email>email2</Email>
  <AddressId xsi:nil="true" />
  <IsActive>0</IsActive>
</e>'

;with sub as 
(
SELECT Tbl.Col.value('.', 'nvarchar(max)') as Value
FROM @xml.nodes('e//text()') Tbl(Col)
)
select * from sub s

But it doesn't include nulls. How can change the code to get the nulls in result?

1 Answer 1

3

You're not getting them because you're selecting from //text()

Try selecting from

SELECT  Tbl.Col.value('.', 'nvarchar(max)') as Value
FROM @xml.nodes('e//*') Tbl(Col)
Sign up to request clarification or add additional context in comments.

1 Comment

I guess you mean following: SELECT NULLIF( Tbl.Col.value('.', 'nvarchar(max)') , '')as Value FROM @xml.nodes('e//*') Tbl(Col)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.