Case-Sensitive Headaches? Mastering Case-Insensitive Text Search in SQLite
Important points to consider:
- The
lower()
function approach only works well for ASCII characters. If you're dealing with characters outside the ASCII range (like ö, é, etc.), it won't provide accurate case-insensitive comparisons. - SQLite's built-in case-insensitivity with
LIKE
(using ICU) is limited to ASCII characters as well.
-- This query selects names where the lowercase version of 'name' is equal to lowercase 'Alice'
SELECT * FROM users WHERE LOWER(name) = lower('Alice');
Using LIKE with ICU enabled (assuming ICU extension is installed):
-- Assuming ICU is enabled, this query will match names regardless of case (alice, Alice, ALICE)
SELECT * FROM users WHERE name LIKE '%Alice%';
Creating an index with LOWER() (improves performance for lower() approach):
CREATE INDEX idx_name_lower ON users (LOWER(name));
-- This query will leverage the index for faster search
SELECT * FROM users WHERE LOWER(name) = 'alice';
Choosing the right method depends on several factors:
- Complexity of your needs: Do you require basic case-insensitivity or something more advanced?
- Performance requirements: How important is query speed for your application?
- Development environment: Are you comfortable writing custom functions or prefer built-in solutions?
sqlite case-insensitive