35

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?

  • 1
    then why add your own %? – Randy May 29 '12 at 16:58
  • how to do it depends on your engine, but obviously you have to escape your own % – Sebas May 29 '12 at 17:00
  • what programming language are you using? – Jeshurun May 29 '12 at 17:01
  • Use the MATCH instead of LIKE. – Hitendra Joshi Mar 9 '17 at 9:50
29
0

If you want a % symbol in search_criteria to be treated as a literal character rather than as a wildcard, escape it to [%]

... where name like '%' + replace(search_criteria, '%', '[%]') + '%'
share | improve this answer | |
  • 1
    thank you, gives column with % sign select * from tablename where Column like '%[%]%' – shaijut Mar 18 '15 at 11:42
13
0

Use an escape clause:

select *  from (select '123abc456' AS result from dual        union all        select '123abc%456' AS result from dual       )  WHERE result LIKE '%abc\%%' escape '\'

Result

123abc%456

You can set your escape character to whatever you want. In this case, the default '\'. The escaped '\%' becomes a literal, the second '%' is not escaped, so again wild card.

See List of special characters for SQL LIKE clause

share | improve this answer | |
12
0

The easiest solution is to dispense with "like" altogether:

Select * from tablewhere charindex(search_criteria, name) > 0

I prefer charindex over like. Historically, it had better performance, but I'm not sure if it makes much of difference now.

share | improve this answer | |
  • 1
    I really like this answer, as it is clearly not vulnerable to SQL Injection, as you've avoided the string concatenation. – StuartQ Sep 23 '13 at 13:02
  • 1
    check out sql-server-performance for a v short comparison. Note that using like 'findme%' can still use an index if it exists. – Scotty.NET Sep 26 '13 at 10:18
5
0

To escape a character in sql you can use !:


EXAMPLE - USING ESCAPE CHARACTERS

It is important to understand how to "Escape Characters" when pattern matching. These examples deal specifically with escaping characters in Oracle.

Let's say you wanted to search for a % or a _ character in the SQL LIKE condition. You can do this using an Escape character.

Please note that you can only define an escape character as a single character (length of 1).

For example:

SELECT *FROM suppliersWHERE supplier_name LIKE '!%' escape '!';

This SQL LIKE condition example identifies the ! character as an escape character. This statement will return all suppliers whose name is %.

Here is another more complicated example using escape characters in the SQL LIKE condition.

SELECT *FROM suppliersWHERE supplier_name LIKE 'H%!%' escape '!';

This SQL LIKE condition example returns all suppliers whose name starts with H and ends in %. For example, it would return a value such as 'Hello%'.

You can also use the escape character with the _ character in the SQL LIKE condition.

For example:

SELECT *FROM suppliersWHERE supplier_name LIKE 'H%!_' escape '!';

This SQL LIKE condition example returns all suppliers whose name starts with H and ends in _ . For example, it would return a value such as 'Hello_'.


Reference: sql/like

share | improve this answer | |
2
0
Select * from table where name like search_criteria

if you are expecting the user to add their own wildcards...

share | improve this answer | |
2
0

You need to escape it: on many databases this is done by preceding it with backslash, \%.

So abc becomes abc\%.

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.

share | improve this answer | |
0
0

Escape the percent sign \% to make it part of your comparison value.

share | improve this answer | |
  • This only works if you use the ESCAPE option of the LIKE operator. There is no default escape character (in SQL Server, anyway). – David R Tribble Oct 28 '15 at 16:20
0
0

May be this one help :)

DECLARE @SearchCriteria VARCHAR(25)SET  @SearchCriteria = 'employee'IF CHARINDEX('%', @SearchCriteria) = 0BEGINSET @SearchCriteria = '%' + @SearchCriteria + '%'ENDSELECT * FROM EmployeeWHERE Name LIKE @SearchCriteria
share | improve this answer | |

Not the answer you're looking for? Browse other questions tagged sql sql-server sql-like or ask your own question.