Search code examples
javasqljpasql-like

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?


Solution

  • 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.