SQL wildcards let you match unknown or variable portions of a character string when filtering data. In Standard SQL the percent sign (%) matches zero or more characters and the underscore (_) matches exactly one character in a LIKE predicate. Dialects extend this set: square brackets ( [a-z] ) or POSIX character classes in SIMILAR TO, caret (^) or exclamation mark (!) for negation inside brackets, and question mark (?) in some systems for a single character. Wildcards are interpreted only inside pattern-matching operators (LIKE, NOT LIKE, ILIKE, SIMILAR TO, REGEXP_LIKE). Outside these contexts they are ordinary characters.Behavior details:- Case sensitivity depends on the operator and collation. LIKE is case sensitive in most databases, while ILIKE (PostgreSQL) or COLLATE NOCASE (SQLite) makes it case insensitive.- Escaping: Use the ESCAPE clause or backslash (dialect-specific) to treat wildcard characters literally.- Index use: Leading wildcards (e.g., '%abc') often prevent index usage, causing full scans.- Unicode: Patterns operate on characters, not bytes, but combining characters can yield unexpected matches.Caveats:- Different dialects support different additional wildcards (e.g., MySQL supports '%' and '_' only; SQL Server also supports [] and [^]).- SIMILAR TO and regular expression functions offer richer pattern syntax and are sometimes confused with simple LIKE wildcards.
pattern
(string) - The pattern containing wildcards to match against.escape_char
(single character, optional) - Defines an escape character to treat %, _ or other special characters literally.LIKE, NOT LIKE, ILIKE, SIMILAR TO, REGEXP_LIKE, ESCAPE clause, COLLATION, INDEXES
SQL-92 (LIKE with % and _)
% matches any sequence of zero or more characters. _ matches exactly one character. Use them together for precise pattern control.
In PostgreSQL use ILIKE. In MySQL or SQLite apply a case-insensitive collation or convert both sides with LOWER().
Leading wildcards usually prevent index usage, forcing full table scans. Trailing or middle wildcards may still allow index use depending on the database.
Yes. Numbers are implicitly cast to strings in the comparison. Ensure the column is stored as text or cast explicitly (e.g., phone::text) for predictable results.