Pages

18/10/2014

[SQL] Escape special characters in LIKE statement

When using the LIKE statement in a SQL query, there are special characters which have a particular meaning such as _ (single character) and % (sequence of characters) which have to be escaped if you intend to actually search for them in the string.

You can do so as:

LIKE 'pattern' ESCAPE 'escape_character'

for example:

SELECT name
FROM mytable
WHERE name LIKE '%John\_D%' ESCAPE '\';

Which will search for any string containing "John_D"

No comments:

Post a Comment

With great power comes great responsibility