Wildcard Matching (Pattern Matching)
tags: #sql
How is pattern matching used?
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards[1] often used in conjunction with the LIKE operator:
- The percent sign (%) represents zero, one, or multiple characters
- The underscore sign (_) represents one, single character
Tip!
You can also combine any number of conditions using logical operators (AND, OR).
How do we use it in practice?
Consider the following:
SELECT *
FROM table_name
WHERE name LIKE 'J%';
Here, this will return all records where the "name" column starts with the letter "J", followed by any number of characters.
Finding records with a specific character in the value
SELECT *
FROM table_name
WHERE name LIKE %J% // can also be a sequence of characters e.g., %son%
...
// OR
...
WHERE ssn LIKE '_ _ _ 1 _'
...
A wildcard character is used to substitute one or more characters in a string. ↩︎