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?