I have an SQL query as below.
Select * from table
where name like '%' + search_criteria + '%'
If search_criteria = 'abc', it will return data containing xxxabcxxxx
which is fine.
But if my search_criteria = 'abc%', it will still return data containing xxxabcxxx
, which should not be the case.
How do I handle this situation?
You need to escape it: on many databases this is done by preceding it with backslash,
\%
.So
abc
becomesabc\%
.Your programming language will have a database-specific function to do this for you. For example, PHP has mysql_escape_string() for the MySQL database.
if you are expecting the user to add their own wildcards...