I have following XML stored in a XML column (called Roles) in a SQL Server database.
<root>
<role>Alpha</role>
<role>Beta</role>
<role>Gamma</role>
</root>
I’d like to list all rows that have a specific role in them. This role passed by parameter.
In case your column is not
XML, you need to convert it. You can also use other syntax to query certain attributes of your XML data. Here is an example…Let’s suppose that data column has this:
… and you only want the ones where
CodeSystem = 2then your query will be:These pages will show you more about how to query XML in T-SQL:
Querying XML fields using t-sql
Flattening XML Data in SQL Server
EDIT
After playing with it a little bit more, I ended up with this amazing query that uses CROSS APPLY. This one will search every row (role) for the value you put in your like expression…
Given this table structure:
We can query it like this:
You can check the SQL Fiddle here: http://sqlfiddle.com/#!18/dc4d2/1/0