sql-servert-sqlescapingcharactersql-like

How can I escape square brackets in a LIKE clause?


I am trying to filter items with a stored procedure using like. The column is a varchar(15). The items I am trying to filter have square brackets in the name.

For example: WC[R]S123456.

If I do a LIKE 'WC[R]S123456' it will not return anything.

I found some information on using the ESCAPE keyword with LIKE, but how can I use it to treat the square brackets as a regular string?


Solution

  • LIKE 'WC[[]R]S123456' 
    

    or

    LIKE 'WC\[R]S123456' ESCAPE '\'
    

    Should work.