SIMILAR TO Sits Between LIKE And Regex
Match whole strings with SIMILAR TO, which keeps LIKE wildcards but adds POSIX alternation, repetition, and bracket classes — while the dot stays literal.
Code
-- SIMILAR TO 像 LIKE 一样只做全串匹配:% 任意串、_ 单字符
SELECT 'SKU-2026-A1' SIMILAR TO 'SKU-%' AS prefix_match,
'SKU-2026-A1' SIMILAR TO 'SKU-____-__' AS shape_match;
-- 但它接受 POSIX 风格的 | 交替、+ ? {m,n} 重复与 [...] 字符类
SELECT 'A1' SIMILAR TO '[A-Z][0-9]' AS bracket_class,
'abc' SIMILAR TO '%(b|d)%' AS alternation,
'aaab' SIMILAR TO 'a+b' AS repetition,
'ac' SIMILAR TO 'ab?c' AS optional_b;
-- 点号不是元字符:SIMILAR TO 的 . 就是字面点
SELECT 'v1.2' SIMILAR TO 'v1.2' AS dot_is_literal,
'v1x2' SIMILAR TO 'v1.2' AS dot_not_wildcard;
-- 反斜杠转义元字符;ESCAPE '' 可关闭转义语义
SELECT '50%' SIMILAR TO '50\%' AS escaped_percent;
-- 无元字符时退化为等值比较;NOT SIMILAR TO 是它的否定形式
SELECT 'abc' SIMILAR TO 'abc' AS plain_equal,
'abd' NOT SIMILAR TO 'abc' AS not_form;How it works
The file is a set of predicates over string literals — no table is referenced, and every statement returns booleans in a single row.
SIMILAR TO starts from the LIKE model: % stands for any run of characters and _ for exactly one, and the pattern must cover the entire string rather than a substring.
Where it goes beyond LIKE
On top of that base it accepts the POSIX constructs the second statement exercises: bracket classes, | alternation, and the repetition forms +, ? and {m,n}. Escaping is available through the default backslash, so 50\% matches a literal percent sign.
Key statements
SELECT 'abc' SIMILAR TO '%(b|d)%' AS alternation,
'aaab' SIMILAR TO 'a+b' AS repetition,
'ac' SIMILAR TO 'ab?c' AS optional_b;
SELECT '50%' SIMILAR TO '50\%' AS escaped_percent;
Interactive Tools for This Topic
Report a problem with this exampleOpens a pre-filled issue on GitHub.