Skip to content

SQL Wildcards

A wildcard is a character in a LIKE pattern that stands for other characters. Every database supports % and _; SQL Server and Microsoft Access add a few more, and Access uses different symbols for the common two.

The examples use the Crew table from the example database.

Wildcard Stands for
% any run of characters, including none
_ exactly one character

% can sit in the middle of a pattern too. To find anyone with a word beginning with P after the first name:

SELECT Name FROM Crew
WHERE Name LIKE '% P%';
Name
Owen Price

Priya Nair is not listed: her P comes first, with no space before it.

To find names whose second letter is o:

SELECT Name FROM Crew
WHERE Name LIKE '_o%';
Name
Tom Hale

SQL Server and Access let a pattern name a set of characters in square brackets. The set matches one character from the list:

SELECT Name FROM Crew
WHERE Name LIKE '[MP]%';
Name
Maya Okafor
Priya Nair

A hyphen gives a range, so '[A-L]%' matches names starting with any letter from A to L; here only Lucy Brennan. A caret at the start of the set reverses it: '[^MP]%' matches the names that do not start with M or P.

Meaning Most databases SQL Server Access
any run of characters % % *
exactly one character _ _ ?
one character from a set [MP] [MP]
one character not in a set [^MP] [!MP]
one character in a range [A-L] [A-L]
one digit #