← Back to SQL, page 2

sql-0023 · Medium mid

How do the LIKE wildcards % and _ work, and how do you search for a literal percent sign or underscore?

In LIKE, % matches any run of characters (including none) and _ matches exactly one character. To match a literal % or _, declare an escape character with ESCAPE and put it before the wildcard, for example LIKE '%50\%%' ESCAPE '\'. Without ESCAPE, a search for '50%' matches '50' followed by anything.

Full answer

LIKE compares a value with a pattern that has two wildcards:

  • % matches zero or more characters. 'Ma%' matches Maya and Ma.
  • _ matches exactly one character. 'R_vi' matches Ravi but not Rvi or Raavi.
sql
CREATE TABLE names (n TEXT);
INSERT INTO names VALUES ('Ravi'),('Rvi'),('Raavi'),('Maya'),('Ma');
SELECT n,
       n LIKE 'Ma%'  AS starts_ma,
       n LIKE 'R_vi' AS r_one_vi
FROM names
ORDER BY n;
nstarts_mar_one_vi
Ma10
Maya10
Raavi00
Ravi01
Rvi00

The trouble starts when the data itself contains % or _. A coupon table has notes like "50% off" and "500 points". Searching for '%50%' cannot tell the literal percent from the wildcard:

sql
CREATE TABLE coupons (code TEXT, note TEXT);
INSERT INTO coupons VALUES ('A1','50% off'),('B2','500 points'),('C3','5 percent'),('NEW_USER','welcome'),('NEWXUSER','typo copy');
SELECT code, note FROM coupons WHERE note LIKE '%50%' ORDER BY code;
codenote
A150% off
B2500 points

Declare an escape character and put it in front of the character you want to match literally:

sql
SELECT code, note FROM coupons WHERE note LIKE '%50\%%' ESCAPE '\' ORDER BY code;
codenote
A150% off

Now \% means "a real percent sign". The same applies to underscores, which are common in codes such as NEW_USER:

sql
SELECT code,
       code LIKE 'NEW_%'               AS plain,
       code LIKE 'NEW\_%' ESCAPE '\'   AS escaped
FROM coupons
WHERE code LIKE 'NEW%'
ORDER BY code;
codeplainescaped
NEWXUSER10
NEW_USER11

Without the escape, 'NEW_%' also matches NEWXUSER, because _ matched the X. PostgreSQL uses backslash as its default escape character for LIKE, so '%50\%%' works there even without the ESCAPE clause; SQLite has no default escape, so the clause is required.

When user input goes into a LIKE pattern, escape % and _ in the input as well as passing it as a parameter. Otherwise a user typing % turns a search into "match everything".

sql
CREATE TABLE names (n TEXT);
INSERT INTO names VALUES ('Ravi'),('Rvi'),('Raavi'),('Maya'),('Ma');
SELECT n,
       n LIKE 'Ma%'  AS starts_ma,
       n LIKE 'R_vi' AS r_one_vi
FROM names
ORDER BY n;

CREATE TABLE coupons (code TEXT, note TEXT);
INSERT INTO coupons VALUES ('A1','50% off'),('B2','500 points'),('C3','5 percent'),('NEW_USER','welcome'),('NEWXUSER','typo copy');
SELECT code, note FROM coupons WHERE note LIKE '%50%' ORDER BY code;

SELECT code, note FROM coupons WHERE note LIKE '%50\%%' ESCAPE '\' ORDER BY code;

SELECT code,
       code LIKE 'NEW_%'               AS plain,
       code LIKE 'NEW\_%' ESCAPE '\'   AS escaped
FROM coupons
WHERE code LIKE 'NEW%'
ORDER BY code;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • % matches any run of characters; _ matches exactly one
  • ESCAPE declares a character that makes the next wildcard literal
  • SQLite needs an explicit ESCAPE clause
  • Escape wildcards in user search input, not only quotes

Common mistakes

  • Searching for codes with underscores and getting extra matches
  • Passing raw user input into a LIKE pattern

Follow-ups

  • Is LIKE case-sensitive on SQLite and PostgreSQL?
  • Which LIKE patterns can use a B-tree index?

Tips

  • Show a pattern with _ catching an extra row; it proves you understand the wildcard