Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape literal percent sign when NO_BACKSLASH_ESCAPES option is enabled?

My company runs MySQL in NO_BACKSLASH_ESCAPES mode. How can I escape a literal % or _ in a LIKE query in this mode? The standard way is \%, but that doesn't work in this mode.

Example: a column has the following values: 5% off, 50% off. The following query works in standard mode but not in NO_BACKSLASH_ESCAPES mode:

SELECT * FROM mytable WHERE mycol LIKE '5\% off' 
like image 801
Kip Avatar asked Feb 16 '11 17:02

Kip


People also ask

How do I escape the percentage sign in MySQL?

You need to escape it: From the tests I did on SQLFiddle . By default the \ is already the ESCAPE , such that just doing: SELECT * FROM tb_table WHERE nome_fantasia LIKE 'name\%name';

How do you escape a percentage in SQL?

You can use the ESCAPE keyword with LIKE . Simply prepend the desired character (e.g. '! ') to each of the existing % signs in the string and then add ESCAPE '!' (or your character of choice) to the end of the query.

What is the use of like in MySQL?

The MySQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.


Video Answer


1 Answers

you need escape

select * from mytable where mycol like '5\% off' escape '\'; 

For a version that works regardless of NO_BACKSLASH_ESCAPES mode, you can use a different character, like pipe:

select * from mytable where mycol like '5|% off' escape '|'; 
like image 186
ajreal Avatar answered Sep 29 '22 03:09

ajreal