Compare strings ignoring case and accents in SQL
Name matching fails on case and accents. Collations let the comparison itself ignore both.
The task
Instead of cleaning the strings, tell the comparison to ignore differences: a CI_AI (case-insensitive, accent-insensitive) collation does that per expression.
The snippet
Written for SQL Server. Versions for PostgreSQL and MySQL are under Variations.
SELECT *
FROM customers c
WHERE c.last_name COLLATE Latin1_General_CI_AI = 'garcia';
Cómo funciona
COLLATE applies only to that comparison, leaving the stored data untouched. Latin1_General_CI_AI ignores case and accents, so 'garcia' = 'García'. The cost is index use: the comparison will not use a plain index on last_name unless the column itself has a matching collation. On PostgreSQL 12+, a nondeterministic ICU collation (CREATE COLLATION ... provider = icu, deterministic = false) gives the same column-level behaviour.
Variations
PostgreSQL (unaccent extension)
PostgreSQLSELECT *
FROM customers c
WHERE unaccent(lower(c.last_name)) = unaccent(lower('garcia'));
MySQL 8 (utf8mb4 column)
MySQLSELECT *
FROM customers c
WHERE c.last_name = 'garcia' COLLATE utf8mb4_0900_ai_ci;
Questions that come up
Why not just LOWER both sides?
LOWER fixes case only. It does nothing about accents, so 'garcia' still misses 'García'. Case- and accent-insensitive collations (or unaccent on PostgreSQL) handle both at once.
Keep reading
Related on SQLly
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.