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

后端 未结 8 1930
被撕碎了的回忆
被撕碎了的回忆 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:24

    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 suppliers
    WHERE 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 suppliers
    WHERE 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 suppliers
    WHERE 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

    0 讨论(0)
  • 2020-12-03 03:24
    Select * from table where name like search_criteria
    

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

    0 讨论(0)
提交回复
热议问题