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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With