Search code examples
sqlsql-server-2012sql-like

SQL Server Like is not working with Square brackets and hyphens


I am trying to do a like for following charecters,

[C0-S12]ECT-PM18052

As metnioned in this post I tried following query,

 WHERE Barcode LIKE '[[C0-S12]%' 

and this returns everything. It completely ignores the left side which is constant. For example I am getting back following results back.

[C0-S12]ECT-PM18052
[C0-S8]ECT-PM18052
[C0-B14]ECT-PM18052
[C0-S17]ECT-PM18052
[C0-B3]ECT-PM18052
[C0-S7]ECT-PM18052
[C0-B12]ECT-PM18052

I have also tried following this post and escaped using '/ /'.

  WHERE Barcode LIKE '\[C0-S12%\'

and this does not return anything atll.


Solution

  • This version works:

    WHERE Barcode LIKE '$[C0-S12]%' ESCAPE '$'
    

    I notice that the \ escape doesn't work on Rextester. It should work, but it doesn't.