TExcelTAD
Home › SQL › SQL Wildcards

SQL Wildcards

A wildcard is a character that stands for other characters in a pattern. Standard SQL's LIKE knows only two; some databases add more, and SQLite offers a second operator, GLOB, with Unix-style wildcards.

Wildcards by database

Symbol Meaning Where
% any number of characters (0 or more) LIKE everywhere
_ exactly one character LIKE everywhere
[abc] one character from the set SQL Server LIKE, SQLite GLOB
[^abc] / [!abc] one character not in the set SQL Server LIKE (^), SQLite GLOB (^)
[a-f] one character in a range SQL Server LIKE, SQLite GLOB
* / ? any characters / one character SQLite GLOB, MS Access LIKE

Combining % and _

Product units that end in "0 g" (100 g, 250 g, …):

sql · try it yourself
SELECT name, unit
FROM products
WHERE unit LIKE '%0 g'
ORDER BY name;
name               | unit
-------------------+------
Cherry Tomatoes    | 250 g
Dark Chocolate Bar | 100 g
Greek Yogurt       | 500 g
Salted Almonds     | 200 g
Sourdough Loaf     | 800 g
Swiss Cheese       | 250 g
Tortilla Chips     | 300 g
(7 rows)

Emails whose user part is exactly a first name, a dot, and a five-letter last name:

sql · try it yourself
SELECT email
FROM customers
WHERE email LIKE '%._____@example.com'
ORDER BY email;
email
-----------------------
amara.costa@example.com
emma.novak@example.com
lucas.silva@example.com
sofia.rossi@example.com
(4 rows)

Character sets with GLOB

GLOB uses * and ? instead of % and _, supports [...] sets, and is case-sensitive:

sql · try it yourself
-- Customers whose city starts with L, M or N
SELECT name, city
FROM customers
WHERE city GLOB '[LMN]*'
ORDER BY city, name;
name            | city
----------------+------------
Amara Costa     | Lagos
Lena Andersen   | Lisbon
Jonas Garcia    | London
Nonna's Kitchen | London
Pixel Studio    | Madrid
Felix Reyes     | Mexico City
Omar Murphy     | Mumbai
Night Owl Books | New York
(8 rows)
sql · try it yourself
-- Product names that contain a digit or a hyphen
SELECT name
FROM products
WHERE name GLOB '*[0-9-]*'
ORDER BY name;
name
---------------------------
Noise-Cancelling Headphones
USB-C Cable
(2 rows)

In SQL Server the same query would be WHERE name LIKE '%[0-9-]%'. In PostgreSQL and MySQL you would use regular expressions (~ or REGEXP).

Searching for a literal % or _

What if the text itself contains % or _? Declare an escape character with ESCAPE:

sql · try it yourself
CREATE TABLE promos (code TEXT);
INSERT INTO promos VALUES ('SAVE10%'), ('SAVE_10'), ('SAVE100');

SELECT code FROM promos WHERE code LIKE 'SAVE_10' ORDER BY code;
SELECT code FROM promos WHERE code LIKE 'SAVE\_10' ESCAPE '\' ORDER BY code;
SELECT code FROM promos WHERE code LIKE '%!%' ESCAPE '!' ORDER BY code;
3 rows affected.

code
-------
SAVE_10
(1 row)

code
-------
SAVE_10
(1 row)

code
-------
SAVE10%
(1 row)

Without ESCAPE, _ in 'SAVE_10' matched any character, so SAVE100 matched too.

Tip: If user input is placed inside a LIKE pattern (a search box, for example), escape % and _ in it — otherwise a user typing % matches everything.

Test yourself

Which SQLite operator supports [0-9] character ranges?

  1. LIKE
  2. GLOB
  3. IN
  4. MATCHES

How do you search for a literal underscore with LIKE?

  1. Use two underscores
  2. Use an ESCAPE character, e.g. 'a\_b' ESCAPE '\'
  3. Put it in double quotes
  4. It is impossible