Martynas Martynas - 4 months ago 8
SQL Question

SQL select statement to return lines containing string not just equal to string

I need SQL statement to return all the lines containing the text entered into a search box and not just the ones that are equal to the text entered.

SELECT * from table WHERE name='" + this.SearchBoxTxt.Text + "'


this only returns the exact matches
I read that you're suppose to use % in order to get the lines containing the string

but

SELECT * from table WHERE name='%" + this.SearchBoxTxt.Text + "%'


only returns the lines containing the % characters themselves
(for example searching for "test" would return "%test%" as a result)

and

SELECT * from table WHERE name=%'" + this.SearchBoxTxt.Text + "'%
SELECT * from table WHERE name='"% + this.SearchBoxTxt.Text + %"'
SELECT * from table WHERE name='" +% this.SearchBoxTxt.Text %+ "'


Don't run at all.

What am I missing?

SMS SMS
Answer

Please use LIKE instead of = operator. Try something the below:

SELECT * from table WHERE name LIKE '%test%'

You should concatenate the strings that gives something like above.

Hope it helps!

Comments