Sub topic 3
Matching a Literal `%` or `_` with `ESCAPE`
This chapter assumes you can write basic SELECT statements, use WHERE, and compare values with operators such as = and >. It focuses on one detail of SQL pattern matching: how to search for a literal percent sign or underscore instead of treating it as a wildcard.
Why `LIKE` needs escaping
The LIKE predicate compares text with a pattern. In a pattern:
%matches any sequence of characters, including an empty sequence._matches exactly one character.
For example:
SELECT id, label
FROM promotions
WHERE label LIKE '10%';This does not search specifically for the text 10%. It matches every label that starts with 10, such as 10% off, 10 dollars, and 100 points.
To search for a character that has a special meaning, prefix it with an escape character. The ESCAPE clause tells SQL which character has that role.
SELECT id, label
FROM promotions
WHERE label LIKE '10!%%' ESCAPE '!';The pattern is interpreted as follows:
| Pattern part | Meaning |
|---|---|
| `10` | The literal text `10` |
| `!%` | A literal percent sign |
| `%` | Any sequence of characters |
This query matches labels beginning with the literal text 10%, such as 10% off.
Escaping an underscore
The same technique works for _. Suppose product codes can contain a literal underscore:
SELECT id, product_code
FROM products
WHERE product_code LIKE 'AB!_100' ESCAPE '!';Here, !_ means a literal underscore. The query matches AB_100, but not ABX100.
Without the escape character, _ would match any one character:
SELECT id, product_code
FROM products
WHERE product_code LIKE 'AB_100';That pattern could match ABX100, AB-100, or AB_100.
Choosing and using the escape character
The character after ESCAPE is the escape character for that pattern. It is usually a character that is unlikely to appear in the searched text, such as ! or ~.
SELECT id, description
FROM products
WHERE description LIKE 'Save 20!%%' ESCAPE '!';The escape character itself can also be escaped by repeating it. For example:
SELECT id, path
FROM files
WHERE path LIKE 'C:!!%' ESCAPE '!';In this pattern:
!!represents one literal!.- The final
%remains a wildcard.
Therefore, the pattern matches paths beginning with C:!.
Escaping user-provided search text
Applications often let users search for arbitrary text. If the search should treat % and _ as ordinary characters, the application must escape them before placing the value in a LIKE pattern. It must also escape the chosen escape character first.
For an escape character of !, the replacement order is important:
- Replace
!with!!. - Replace
%with!%. - Replace
_with!_.
The query should still use a parameter rather than concatenating user input into SQL:
SELECT id, label
FROM promotions
WHERE label LIKE :pattern ESCAPE '!';The parameter value should contain the already-escaped pattern. Parameter binding prevents SQL injection, while pattern escaping controls the meaning of LIKE wildcards.
Portability and performance
ESCAPE is part of standard SQL, but details such as the default escape character and string-literal rules differ among database systems. Explicitly writing ESCAPE '!' makes the intended pattern clearer and avoids relying on a database-specific default.
A pattern beginning with ordinary text, such as '10!%%', may be able to use an index on the searched column. A pattern beginning with % usually cannot use a normal B-tree index efficiently because the database does not know the beginning of the value. Always confirm performance with your database's query plan tools.
Exercise
Write a query that finds product codes beginning with the literal text AB_, followed by any characters.
Show answer
SELECT id, productcode FROM products WHERE productcode LIKE 'AB!_%' ESCAPE '!';