\ functions as an escape character in LIKE by default. From the manual for LIKE : Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”.
MySQL recognizes the following escape sequences. \0 An ASCII NUL (0x00) character. \' A single quote (“'”) character. \" A double quote (“"”) character.
About Escaping File Paths Characters to be escaped include the backslash (\, because it is the escaping character) and the double quotes ("). Both of these characters can be relevant in file paths.
\
functions as an escape character in LIKE
by default.
From the manual for LIKE
:
Because MySQL uses C escape syntax in strings (for example, “\n” to represent a newline character), you must double any “\” that you use in LIKE strings. For example, to search for “\n”, specify it as “\\n”. To search for “\”, specify it as “\\\\”; this is because the backslashes are stripped once by the parser and again when the pattern match is made, leaving a single backslash to be matched against.
You can change this by specifying another escape character, as in:
SELECT * FROM `titles` WHERE title LIKE 'test\\' ESCAPE '|'
Actually, all previous answers have been mangled somewhere. As you can see in the link provided by Karoly Horvath, whose significant bit is reproduced by Explosion Pills, the correct way of searching for 1 backslash (\) is to use 4 backslashes at once (\\\\).
By the way, to show above that single backslash I had to use two at once and to show those four I had to use eight.
LIKE
accepts two wildchar characters, %
and _
.
To be able to match these characters, escaping can be used: \%
, \_
. This also means that if you want to match \
, it has to be escaped as well.
All this is documented in the manual.
For finding a \
within a text field I had to escape the \
twice, else the %
at the end was found:
SELECT * FROM `table` where `field` LIKE '%\\\%';
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