SQL case-insensitive comparison
UPPER(col) = UPPER(?) kills indexes. Every engine has a collation-level answer that keeps matching fast and locale-correct.
- Kategorie
- Strings
- Returns
string- In this category
- 32 of 172
Syntax
col = 'value' COLLATE ci_collation
col ILIKE 'value'
Each block is a separate form. The support table and the examples show which engine takes which.
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Ja | Supported under this name. |
| PostgreSQL | Ja | Supported under this name. |
| MySQL | Ja | Supported under this name. |
| SQLite | Ja | Supported under this name. |
| Oracle | Ja | Supported under this name. |
Beispiele
PostgreSQL
SELECT * FROM users WHERE email ILIKE '[email protected]';
SQLite
SELECT 'abc' = 'ABC' COLLATE NOCASE AS eq; -- 1
Oracle
SELECT * FROM users WHERE email = '[email protected]'; -- with NLS_COMP=LINGUISTIC + NLS_SORT=BINARY_CI
Gut zu wissen
- SQL Server: default collations are already case-insensitive; pick a _CS collation per column or query for the rare case-sensitive match.
- PostgreSQL: = is case-sensitive; use ILIKE, the citext extension type, or a *_ci ICU collation (PG 12+).
- MySQL: default collations end in _ci (case-insensitive); _bin and utf8mb4_0900_as_cs are the case-sensitive ones.
- SQLite: = is case-sensitive while LIKE is case-insensitive for ASCII only; declare columns COLLATE NOCASE to make = insensitive too.
Questions about Case-insensitive comparison
Is UPPER(col) = UPPER('x') wrong?
It works but disables a plain index on col and can be locale-wrong (Turkish I). A case-insensitive collation or citext does the same job and can use an ordinary index; PostgreSQL's ILIKE matches case-insensitively but cannot use a plain b-tree index either.
Why does my WHERE clause behave differently in SQLite?
SQLite's LIKE ignores case for ASCII only, while = is exact. Adding COLLATE NOCASE to the column definition lines them up.
Do case-insensitive collations ignore accents?
SQL Server's _AI collations and MySQL's _ai_ci do; _AS/_as_cs keep accents significant. Pick the combination deliberately per column.
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.