How to know if a string is part of an XML?

I have a table named "TempTable" with an XML column named "XML_Collection". Now I want to know if the string "My Valentine" appears in any of those XML rows.

I used the string manipulator: PATINDEX

SELECT * from TempTable WHERE PATINDEX('%My Valentine%', convert(varchar(max),XML_Collection)) > 0

Important notes:
  • be sure to convert the XML type to varchar(max) first before using any string manipulator functions
  • if PatIndex == 0 then that string pattern does not exist
  • if PatIndex > 0 then that string pattern exists

No comments: