Loading lessons...
LIKE & Wildcards
LIKE & Wildcards
The LIKE operator searches for a pattern in a text column. It works with wildcard characters.
The % wildcard (any characters)
SELECT * FROM Customers
WHERE CustomerName LIKE 'A%';
Finds names that start with A ("Alfreds", "Ana", "Antonio", ...).
SELECT * FROM Customers
WHERE CustomerName LIKE '%a';
Finds names that end with a.
SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';
Finds names that contain "or" anywhere.
The _ wildcard (exactly one character)
SELECT * FROM Customers
WHERE City LIKE '_ondon';
Finds "London" (any single character followed by "ondon").
Start and end at the same time
SELECT * FROM Customers
WHERE CustomerName LIKE 'A%y';
Starts with A and ends with y.
NOT LIKE
SELECT * FROM Customers
WHERE City NOT LIKE 'B%';
Cities that do not start with B.
Wildcard reference
| Wildcard | Matches |
|---|---|
| % | zero or more characters |
| _ | exactly one character |
TL;DR
- LIKE searches for patterns.
- % matches any number of characters.
- _ matches exactly one character.
- Combine them and use NOT LIKE to exclude.