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
 General SQL Server Forums
 New to SQL Server Programming
 Using LIKE with a parameterized query

Author  Topic 

jplace
Starting Member

1 Post

Posted - 2012-11-18 : 18:33:07
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??

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-11-18 : 18:40:31
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.

Jim


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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-19 : 01:33:00
here's link to ParseValues

http://visakhm.blogspot.in/2010/02/parsing-delimited-string.html

another way is this

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

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -