0

Possible Duplicate:
Parameterized Queries with LIKE and IN conditions

I try in my VB.net application to search persons on characters put in a text box by a parameter in sql DB. Something like-

select * from employee where name like '@name%'

but it doesn't work!

Can somebody help me?

Community
  • 1
  • 1
ramajoud1
  • 31
  • 4

3 Answers3

3

For a starts-with match:

SELECT * FROM employee WHERE name LIKE @name + '%'

For a contains match:

SELECT * FROM employee WHERE name LIKE '%' + @name + '%'

You want to combine the string '%' with the value of @name: currently you are using the string '@name%' which of course is not what you want to search for.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
2

If you want to handle this in SQL you can also do the following:

select * from employee where name like @name +'%'

Edit: Kieren Johnson beat me to this answer and has explained further

WT_W
  • 235
  • 1
  • 3
  • 11
0

you have to put the % at the end of your named parameter.

select * from employee where name like @name

addParameter("@name", "obama%") //this isn't actually a real function (because I don't know how you would call in in VB, but I think you see what i mean ;-))

also you should remove the ' around your named parameter

Fender
  • 3,055
  • 1
  • 17
  • 25