SQL 'LIKE' query using '%' where the search criteria contains '%'

后端 未结 8 1959
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 02:29

I have an SQL query as below.

Select * from table 
where name like \'%\' + search_criteria + \'%\' 

If search_criteria = \'abc\', it will r

8条回答
  •  难免孤独
    2020-12-03 03:09

    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

提交回复
热议问题