StuartO StuartO - 4 months ago 7
SQL Question

Using SQL to query an XML data column

I'm having issues querying XML data stored in a SQL Server 2012 database. The node tree I wish to query is in the following format -

<eForm>
<page id="equalities" visited="true" complete="true">
<Belief>
<item selected="True" value="Christian">Christian</item>
<item selected="False" value="Jewish">Jewish</item>
...
</Belief>
</page>
</eForm>


What I would like to do is return the value attribute of the item node where the selected attribute is equal to true. I've read several tutorials on querying XML in SQL but can't seem to get the code right.

Thanks
Stu

Answer

DEMO

SELECT [value].query('data(eForm/page/Belief/item[@selected="True"]/@value)')
FROM test
Comments