StuartO StuartO - 1 year ago 61
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 -

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

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.


Answer Source


SELECT [value].query('data(eForm/page/Belief/item[@selected="True"]/@value)')
FROM test
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download