LIKE Escapes Underscore Wildcards

Match literal underscores and percent signs in LIKE patterns by escaping them, or the wildcards silently match characters you did not intend.

SqlData Fetching

Code

snippet
-- LIKE 里 _ 匹配任意单字符、% 匹配任意序列;字面匹配必须转义通配符
SELECT
    'user_2' LIKE 'user_2'  AS wildcard_eats_literal,  -- true:_ 被当通配符
    'userX2' LIKE 'user_2'  AS unintended_match,       -- true:误命中
    'user_2' LIKE 'user^_2' ESCAPE '^' AS literal_hit, -- true:^ 转义后按字面匹配
    'userX2' LIKE 'user^_2' ESCAPE '^' AS literal_miss -- false:不再误命中
FROM (VALUES (1)) AS t(x);

-- % 同理;默认转义符是反斜杠,数据里反斜杠多时换成不冲突的字符更稳
SELECT
    '50%'  LIKE '50^%' ESCAPE '^' AS percent_literal,   -- true
    '50x'  LIKE '50^%' ESCAPE '^' AS percent_miss,      -- false
    '50%%' LIKE '50^%^%' ESCAPE '^' AS double_percent,  -- true:连续 % 也按字面
    'a^b'  LIKE 'a^^b' ESCAPE '^' AS escape_char_itself -- true:^^ 匹配字面 ^
FROM (VALUES (1)) AS t(x);

How it works

Two SELECT statements evaluate LIKE expressions against fixed literals, so every comparison’s truth value lands in one output row. The first statement contrasts the unescaped pattern 'user_2' — where the _ in the pattern matches any single character, so both 'user_2' (wildcard_eats_literal: true) and 'userX2' (unintended_match: true) come back true — with the escaped form 'user^_2' ESCAPE '^', where ^_ matches only a literal underscore: literal_hit stays true for 'user_2' while literal_miss falls to false for 'userX2'.

The second statement repeats the exercise for %, which matches any sequence: '50^%' ESCAPE '^' matches the literal string 50% (percent_literal: true) but not 50x (percent_miss: false); consecutive percent signs each need their own escape, so '50^%^%' matches 50%% (double_percent: true); and the escape character itself is matched by doubling it — 'a^^b' matches a^b (escape_char_itself: true).

What to watch for

  • The default escape character is the backslash; the file’s comment recommends ESCAPE '^' when the data being matched contains many backslashes, so the pattern does not fight the data.
  • Both statements select from a (VALUES (1)) stub, so the snippet runs without any table and the results depend only on the quoted literals.

Using it

Each comparison is aliased (wildcard_eats_literal, unintended_match, literal_hit, literal_miss, percent_literal, percent_miss, double_percent, escape_char_itself), so the eight-column truth table reads directly off the result row.

Key statements

SELECT
'user_2' LIKE 'user_2'  AS wildcard_eats_literal,  -- true:_ 被当通配符
'userX2' LIKE 'user_2'  AS unintended_match,       -- true:误命中
'user_2' LIKE 'user^_2' ESCAPE '^' AS literal_hit, -- true:^ 转义后按字面匹配
'userX2' LIKE 'user^_2' ESCAPE '^' AS literal_miss -- false:不再误命中
FROM (VALUES (1)) AS t(x);
SELECT
'50%'  LIKE '50^%' ESCAPE '^' AS percent_literal,   -- true
'50x'  LIKE '50^%' ESCAPE '^' AS percent_miss,      -- false
'50%%' LIKE '50^%^%' ESCAPE '^' AS double_percent,  -- true:连续 % 也按字面
'a^b'  LIKE 'a^^b' ESCAPE '^' AS escape_char_itself -- true:^^ 匹配字面 ^
FROM (VALUES (1)) AS t(x);

Interactive Tools for This Topic