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.
The two everyone supports
Section titled “The two everyone supports”| 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 CrewWHERE 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 CrewWHERE Name LIKE '_o%';| Name |
|---|
| Tom Hale |
Character sets in SQL Server and Access
Section titled “Character sets in SQL Server and Access”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 CrewWHERE 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.
The symbols in each database
Section titled “The symbols in each database”| 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 | # |