How can I escape square brackets in a LIKE clause?

Travis picture Travis · Jan 13, 2009 · Viewed 166.2k times · Source

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 I do not understand how to use it to treat the square brackets as a regular string.

Answer

Otávio Décio picture Otávio Décio · Jan 13, 2009
LIKE 'WC[[]R]S123456' 

or

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

Should work.