0

If I have the XML like the following: (As an XML variable names @nodes)

<Nodes>
    <Item>Item 1</Item>
    <Item>Item 2</Item>
    <Item>Item 3</Item>
</Nodes>

And I also have the index of the item I am looking for (example 2). How can I get the value 'Item 2' using a query?

I have been trying something like this:

  SELECT
@result = @nodes.value('(/Nodes/Item)[2]', 'nvarchar(255)')

But all I can seem to get back is NULL

1 Answer 1

1

Your approach works for me.

Here's how I assigned the XML to the variable.

declare @nodes as xml 
set @nodes = '<Nodes>
    <Item>Value 5</Item>
    <Item>Localhost</Item>
    <Item>Unrouteable - 10.x.x.x</Item>
    <Item>Unrouteable - 172.16-31.x.x</Item><Item>Unrouteable - 192.168.x.x</Item>
</Nodes>'

SELECT
@nodes.value('(/Nodes/Item)[2]', 'nvarchar(255)')
Sign up to request clarification or add additional context in comments.

1 Comment

Ah damn.. I changed a couple things when I was pasting my example.. guess I must have fixed it then. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.