How do we search for patterns containing the actual characters "%" or "_"?


#1

Question

When using SQL LIKE operators, how do we search for patterns containing the actual characters “%” or “_”?

Answer

When searching for a pattern containing the specific characters % or _, we can utilize the escape character \, similarly to its use in Python.

If we want to search for these specific characters, we can simply add the escape character immediately before them.

Example

/* 
In this pattern, we use an escape character before '%'.
This will only match "%" and not be used like the
wildcard character.

This query will match any titles that end with
' 100%'.
*/

SELECT *
FROM books
WHERE title LIKE '% 100\%';