I’ve been fighting this for a while, seems I’m close but not quite there. I have a column in a database that looks like this:
<document>
<items>
<item name="one">one is the first number</item>
<item name="two">two is the second number</item>
</items>
</document>
In this example I need to query and return ‘two is the second number’. I’d also like to do this without creating a temp table. Currently I have:
create table #test (item1 xml)
insert into #test (item1)
values ('<document> <items> <item name="one">one is the first number</item> <item name="two">two is the second number</item> </items> </document>')
select item1.value('(/document/items/item)[2]', 'nvarchar(max)') from #test
select item1.query('/document/items/item[@name="two"]') from #test
The first select returns the correct value but I need to know that it’s the 2nd ‘index’
The second returns what I want but it returns the entire node two..
What am I missing? And, is there a simple way to use the XML without converting to a temp table?
You can use a variable with datatype XML.
Or you can cast your string to XML in the query.
Your first query uses
.value()which is correct and your second query has the correct XQuery expression. When using.value()you need to use a XQuery expression that returns a single value. This will give you all item nodes where@nameis two/document/items/item[@name="two"]). Adding[1]at the end makes sure that you will only get the first occurrence in the XML where@nameis two.