SQL LIKE operator
Pattern matching where percent matches any run of characters and underscore matches exactly one.
- Categoría
- Condicional
- Returns
boolean- In this category
- 25 of 43
Sintaxis
col LIKE 'a%' | col NOT LIKE 'a%' | col LIKE 'a%' ESCAPE '\'
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sí | Supported under this name. |
| PostgreSQL | Sí | case-sensitive; ILIKE for the insensitive form |
| MySQL | Sí | Supported under this name. |
| SQLite | Sí | LIKE folds ASCII case only; PRAGMA case_sensitive_like changes it |
| Oracle | Sí | case-sensitive unless NLS_COMP=LINGUISTIC with a case-insensitive NLS_SORT |
Ejemplos
Any engine
SELECT name FROM products WHERE name LIKE 'tee-%';
PostgreSQL
SELECT name FROM products WHERE name ILIKE 'tee-%';
Conviene saberlo
- NULL on either side yields UNKNOWN, so NOT LIKE drops NULL rows too, just like an = comparison would.
- Case sensitivity differs: PostgreSQL and Oracle LIKE are case-sensitive (PostgreSQL has ILIKE), SQLite folds ASCII only ('A' LIKE 'a' is true, 'Ä' LIKE 'ä' is false), MySQL and SQL Server follow the column collation.
- T-SQL adds bracket classes: LIKE '[0-9]%' matches a leading digit, and LIKE '[%]' matches a literal percent. The other four engines treat brackets as ordinary characters.
- A leading % defeats B-tree index use; full-text or trigram indexes cover those cases.
Questions about LIKE
How do I make LIKE case-insensitive?
ILIKE in PostgreSQL; SQLite's LIKE already folds ASCII case; MySQL and SQL Server depend on the column collation.
Does LIKE match NULL?
No: NULL LIKE anything is UNKNOWN, and the row is filtered out even with NOT LIKE.
How do I match a literal percent sign?
Use the ESCAPE clause: col LIKE '100!%' ESCAPE '!'.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.