Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Easy way to get a "like" effect on xml?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

194 Posts

Posted - 05/10/2011 :  10:51:30  Show Profile  Reply with Quote
Is there an easy way to run a select statement with a like clause against column(s) of data type xml?

This is just for a quick check on something, so if there's going to be significant overhead involved it's not worth doing.

Flowing Fount of Yak Knowledge

5155 Posts

Posted - 05/10/2011 :  11:08:37  Show Profile  Reply with Quote
There is the exist() method, which you can use together with the xquery functions "contains" or "substring". (Contains is like a charindex function in SQL).

Alternatively, you can cast the XML to varchar and then use the like operator. Most likely you do not want to cast the whole XML column into varchar - it might be 2G Bytes long!!

Examples of both these approaches are on this MSDN page: Also, all these searches are case-sensitive in XML even if our database/table collation is case-insensitive.
Go to Top of Page

Most Valuable Yak

15732 Posts

Posted - 05/10/2011 :  11:09:27  Show Profile  Visit robvolk's Homepage  Reply with Quote
WHERE CAST(xmlCol AS nvarchar(max)) LIKE N'%something%'

Go to Top of Page

Posting Yak Master

194 Posts

Posted - 05/10/2011 :  11:16:09  Show Profile  Reply with Quote
Thanks guys. For some reason I thought you weren't allowed to cast xml to character data types, but on reflection I think that only applies to ntext.
Go to Top of Page

Starting Member

1 Posts

Posted - 10/31/2013 :  09:36:22  Show Profile  Reply with Quote
robvolk's answer worked great for me
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000