Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape _ in SQL Server? [duplicate]

Tags:

sql-server

I am trying to escape the underscore character in a LIKE Statement. I have tried to use the ESCAPE keyword as follows:

COLUMNNAME NOT LIKE '%[\_]xyz%' ESCAPE '\' 

but it doesn't work. It is still filtering out %xyz% when I really want it to filter out %_xyz%.

If not by the ESCAPE keyword, how else can this be accomplished?

Any help is appreciated.

like image 788
James Thomas Avatar asked Feb 19 '10 23:02

James Thomas


People also ask

How do I ignore duplicate records in SQL query?

Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.

How do I skip a single quote in SQL?

The simplest method to escape single quotes in SQL is to use two single quotes. For example, if you wanted to show the value O'Reilly, you would use two quotes in the middle instead of one. The single quote is the escape character in Oracle, SQL Server, MySQL, and PostgreSQL.


1 Answers

Just this should work:

COLUMNNAME NOT LIKE '%[_]xyz%' 

You don't need the ESCAPE here. What you wrote should also work.

If you do want to use ESCAPE you could do this:

columnname NOT LIKE '%\_xyz%' ESCAPE '\'; 

Documentation on escape characters is here.

like image 184
Mark Byers Avatar answered Sep 27 '22 21:09

Mark Byers