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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Easy way to get a "like" effect on xml?

Author  Topic 

mattt
Posting Yak Master

194 Posts

Posted - 2011-05-10 : 10:51:30
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.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-05-10 : 11:08:37
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: http://msdn.microsoft.com/en-us/library/ms345117(v=sql.90).aspx. Also, all these searches are case-sensitive in XML even if our database/table collation is case-insensitive.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-05-10 : 11:09:27
SELECT * FROM myTable
WHERE CAST(xmlCol AS nvarchar(max)) LIKE N'%something%'

Go to Top of Page

mattt
Posting Yak Master

194 Posts

Posted - 2011-05-10 : 11:16:09
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

jimguyer
Starting Member

1 Post

Posted - 2013-10-31 : 09:36:22
robvolk's answer worked great for me
Go to Top of Page
   

- Advertisement -