Strings

Case-insensitive search (ILIKE / LOWER / collation)

3 min read

“Find ‘acme’ whether it’s stored as ACME or Acme.” The portable approach is to lower-case both sides:

SELECT * FROM companies
WHERE LOWER(name) = LOWER(:q);          -- exact, case-insensitive

SELECT * FROM companies
WHERE LOWER(name) LIKE LOWER(:q) || '%'; -- prefix search

Dialect shortcuts:

Keep it index-friendly. WHERE LOWER(name) = LOWER(:q) can’t use a plain index on name. Add a functional index on the expression (Postgres/SQLite: CREATE INDEX ON companies (LOWER(name));), or store a normalized name_lower column and index that. For accent-insensitive matching too, use a case- and accent-insensitive collation (e.g. Postgres citext/ICU collations, und-ci-ai).

Open the full version (with copy buttons) ↗

← All recipes