1

I have table with an XML column which looks like the following. The XML column has some encoded XML inside unencoded XML.

OrderID    UserDefXML
      1    <Order><OrderDetail><LineItem><ItemName>XYZ</ItemName><Custominfo> &lt;PrimaryName&gt;STACKOVERFLOW &lt;/PrimaryName&gt;</Custominfo></LineItem></OrderDetail></Order>      

I need to extract all the rows which has PrimaryName LIKE 'STACK%'. What should the query be like for this?

1 Answer 1

2

You can convert the encoded XML string -the content of Custominfo element- to XML data type first, for example :

SELECT t.*, x.lineItem.value('Custominfo[1]','varchar(max)') 'CustomInfo'
FROM YourTableName t
CROSS APPLY t.UserDefXML.nodes('/Order/OrderDetail/LineItem') as x(lineItem)
WHERE 
  CONVERT(XML, x.lineItem.value('Custominfo[1]','varchar(max)')).value('PrimaryName[1]','varchar(max)') 
  LIKE 'STACK%'

Sqlfiddle Demo

Output :

enter image description here

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

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.