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
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using LIKE with a parameterized query
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 11/18/2012 :  18:33:07  Show Profile  Reply with Quote
I am trying to accomplish something like...

SELECT ItemID, ItemNumber, ItemDesc FROM ITEMS WHERE ItemDesc LIKE '%variable%'

The problem is I am using a parameterized query, so it is structure like...

rsItems_cmd.CommandText = "SELECT ItemID, ItemNumber, ItemDesc FROM ITEMS WHERE ItemDesc LIKE ?"

and then passing the parameter...

rsItems_cmd.Parameters.Append rsItems_cmd.CreateParameter("param1", 200, 1, 50, rsItems__Variable

(Where rsItems__Variable is a string of the value passed)

How do I use a LIKE command when passing a text string as a parameter??

Flowing Fount of Yak Knowledge

2875 Posts

Posted - 11/18/2012 :  18:40:31  Show Profile  Reply with Quote
Try WHERE ItemDesc LIKE '%'+rsItems_Variable + '%'
This won't work if your paramater looks like 'A,B,C' (A delimited list).
If that's the case, look at parsing the string and then joining to it using the function ParseValues, which can be found on this site.


Everyday I learn something that somebody else already knew
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 11/19/2012 :  01:33:00  Show Profile  Reply with Quote
here's link to ParseValues

another way is this

WHERE ','+rsItems_Variable + ',' LIKE '%,' + ItemDesc + ',%'

SQL Server MVP

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