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
 General SQL Server Forums
 New to SQL Server Programming
 Using LIKE with a parameterized query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jplace
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??

jimf
Flowing Fount of Yak Knowledge

USA
2869 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.

Jim


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

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 11/19/2012 :  01:33:00  Show Profile  Reply with Quote
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
  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.05 seconds. Powered By: Snitz Forums 2000