4

I have this XML in SQL Server

<data>
<add key="images" value="image/path/img.gif" />
<data>

I want to select the value attribute of every "add" node with key = "images"

What i have now is:

SELECT ID, Data from Items 
where Data.value('(//data/add[@key="images"]/@value)[1]', 'nvarchar') Like '%img%'

Any suggestions?

2 Answers 2

4

What you have works fine if you just specify a size for your nvarchar retrieved from the XML.

SELECT ID, Data
from Items 
where Data.value('(//data/add[@key="images"]/@value)[1]', 'nvarchar(100)') Like '%img%'

Here I have specified 100 you may set it to something more appropriate for your situation. Without the size the column will have size 1.

Sign up to request clarification or add additional context in comments.

Comments

2

Use this XPath expression:

(//data
    /add
      [@key="images"]
         /@value
            [contains(.,"img")]
 )
 [1]

Comments

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.