Claudio
Claudio

Reputation: 1234

Exact match with sql like and the bind

I have a bind in the SQL query

SELECT * FROM users WHERE name LIKE '%?%'

the bind set the ?.

Now, if i want to search with like method everything work but if, without change the sql, i want to search the exact match i dont now how to do.

I tried some regexp int the textbox es: _jon \jon\ [jon] and some others but nothing work properly.

Any ideas?

Upvotes: 0

Views: 2572

Answers (3)

Donnie
Donnie

Reputation: 46903

Change your query to

select * from users where name like '?'

If you want to do a wildcard match, put the wildcards as part of the string that you're binding to the variable. If you don't want to do a wildcard match, then don't.

Note that like and = have the same performance except when your wildcard character is first in the string (for example, '%bob') as in that case the query optimizer can't use indexes as well to find the row(s) that you're looking for.

Upvotes: 2

Wadih M.
Wadih M.

Reputation: 13452

What is keeping you from changing the SQL?

The Like condition is for 'similar' matches, while the '=' is for exact matches.

Upvotes: 0

chris
chris

Reputation: 9993

you can't search an exact match if the sql contains % symbols, as they are wildcards. you'll need to change the sql to

select * from users where name = '?'

for an exact match

(you can also use select * from users where name like '?' but that's more inefficient)

Upvotes: 0

Related Questions