SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

mattt
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.

sunitabeck
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: 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

USA
15678 Posts

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

Go to Top of Page

mattt
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

jimguyer
Starting Member

USA
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  
 New 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.04 seconds. Powered By: Snitz Forums 2000