79

Consider this QUERY (DEMO IS HERE)

(SELECT * FROM `titles` where title = 'test\\')
UNION ALL
(SELECT * FROM `titles` where title LIKE 'test\\\\')

Output:

| ID | TITLE |
--------------
|  1 | test\ |
|  1 | test\ |

QUESTION:

Why no extra (\) required for (=) but for (like) additional \\ is required? Its clear that MySQL escaped the (test\) with (test\\) then using (test\\\\) is logical for LIKE.

Table information:

CREATE TABLE IF NOT EXISTS `titles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `titles`
--

INSERT INTO `titles` (`id`, `title`) VALUES
(1, 'test\\');
1

5 Answers 5

146

\ 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 '|'
2
  • 4
    Thanks, satisfying answer :) (+1 for ESCAPE also)
    – user1646111
    Feb 17, 2013 at 22:07
  • 2
    For added clarity, here is an example of what the query should look like if you're trying to escape the backslash and have changed the ESCAPE character to pipe: SELECT * FROM table WHERE column LIKE '%|\%' ESCAPE '|'
    – mcmacerson
    Sep 9, 2017 at 16:32
70

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.

8
  • 1
    This is right. As per mysql documentation : 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. [...] To search for “\”, specify it as “\\\\”
    – OlivierH
    Feb 17, 2015 at 9:29
  • I have corrected the answer by Karoly Horvath to show the correct number of backslashes.
    – LSerni
    Apr 7, 2015 at 13:06
  • Thanks for the tip
    – watermelon
    Jan 5, 2017 at 19:02
  • 1
    If your query is embedded in Java code, you'll need 8 backslashes.
    – Alex R
    Oct 4, 2018 at 22:43
  • 1
    To add to the party, if you're coding the escaped sequence into string that needs escaped slashes, you'll potentially be typing up to 8 of them! Enjoy!
    – Saeven
    Sep 18, 2019 at 15:34
9

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 '%\\\%';
2
  • 1
    According to the manual, the question itself, and two other answers here, 4 backslashes are required. Are you sure this works correctly with 3 backslashes? If so, that is intriguing. Apr 18, 2019 at 22:19
  • I can confirm that (at least on MariaDB v10.0.33) 3 backslashes works the exact same way as 4 backslashes.
    – dcsmith926
    Jun 10, 2021 at 14:17
8

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.

1
  • Thanks for additional Info.
    – user1646111
    Feb 17, 2013 at 22:08
-1

If you wanted to remove the (') from the database column (Test's) and replace it with apostrophe (Test's) then you can use the following query.

SELECT replace(column_name, "\\'", "'") FROM table where column_name  LIKE "%\\\%";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.