Sub topic 10
Matching Patterns with LIKE and ILIKE
Assume you know basic SELECT and WHERE clauses, PostgreSQL text values, and NULL. This chapter explains how to filter text when you know a pattern rather than an exact value.
How LIKE patterns work
LIKE compares a text value with a pattern. The pattern can contain two special wildcards:
| Wildcard | Meaning |
|---|---|
| `%` | Zero or more characters |
| `_` | Exactly one character |
The pattern must match the whole value. For example, LIKE 'phone%' matches values that start with phone, while LIKE '%phone%' matches values containing phone anywhere.
WITH products(name) AS (
VALUES
('Phone Case'),
('phone charger'),
('Headphones'),
('Tablet')
)
SELECT name
FROM products
WHERE name LIKE 'Phone%';This returns Phone Case, but not phone charger, because LIKE is case-sensitive. It also does not return Headphones, because the pattern begins at the start of the value.
Use _ when one character may vary:
WITH codes(code) AS (
VALUES ('A12'), ('AB2'), ('A123'), ('A1')
)
SELECT code
FROM codes
WHERE code LIKE 'A_2';The result is A12. The pattern requires three characters: A, any one character, and 2.
Case-insensitive matching with ILIKE
ILIKE is PostgreSQL's case-insensitive version of LIKE. It is useful when users should find results regardless of capitalization.
WITH products(name) AS (
VALUES
('Phone Case'),
('phone charger'),
('HEADPHONES'),
('Tablet')
)
SELECT name
FROM products
WHERE name ILIKE '%phone%';This returns all three values containing phone, regardless of their case.
ILIKE follows the database's collation and locale rules. For ordinary English text, its behavior is usually straightforward. For international text, case conversion can depend on the database configuration, so test important searches with representative data.
Escaping wildcard characters
Sometimes % or _ is part of the text you want to find, not a wildcard. PostgreSQL uses a backslash as the default escape character. You can make that choice explicit with ESCAPE.
WITH labels(label) AS (
VALUES ('10% discount'), ('100 percent'), ('10 items')
)
SELECT label
FROM labels
WHERE label LIKE E'10\\%%' ESCAPE E'\\';The pattern means: 10, then a literal %, then any remaining characters. It returns 10% discount.
A NULL value never matches LIKE or ILIKE. The comparison result is NULL, not true, so the row is not selected by WHERE.
> callout note > Do not add % automatically to every search. name LIKE 'Ann%' searches for a prefix, while name LIKE '%Ann%' searches anywhere and can match much more data.
Common errors
- Forgetting that
LIKE 'cat'is an exact, case-sensitive whole-value match. - Using
_when you mean any number of characters. Use%for that. - Expecting
ILIKEto matchNULL. Filter missing values separately withIS NULL. - Treating user input as a ready-made pattern. User-supplied
%and_act as wildcards unless you escape them.
Exercise
Which rows match ILIKE 'a_%' from the values ('Amy', 'a1', 'AL', 'boat')?
Show answer
Amy, a1, and AL match. The pattern requires a value beginning with a, followed by at least one character; ILIKE ignores case.