Explain Codes LogoExplain Codes Logo

Sql SELECT LIKE (Insensitive casing)

sql
case-insensitivity
dbms
sql-queries
Nikita BarsukovbyNikita Barsukov·Aug 22, 2024
TLDR

To ensure a case-insensitive LIKE query, apply LOWER or UPPER functions in SQL:

SELECT * FROM your_table WHERE LOWER(column_name) LIKE LOWER('%search_value%');

Employing this technique, SQL performs a case comparison on a unified level, negating the typical case sensitivity.

Deep dive: alternate solutions and considerations

Adapting to your Database Management System (DBMS)

Different DBMS have distinct methods handling case sensitivity issues. For instance:

  • PostgreSQL employs the ILIKE operator for inherent case-sensitive search.

    SELECT * FROM your_table WHERE column_name ILIKE '%search_value%'; -- Ain't no case high enough to keep me from getting to you!
  • SQL Server users might use the COLLATE clause to specify case insensitivity right in the query.

    SELECT * FROM your_table WHERE column_name LIKE '%search_value%' COLLATE SQL_Latin1_General_CP1_CI_AS; -- Collation for the nation
  • MySQL allows you to alter the column collation to utf8_general_ci, achieving natural case insensitivity without modifying the query.

Performance implications

Frequently employing LOWER or UPPER could hamper index optimizations, since these functions necessitate a function-based index for optimised execution. On the other hand, collations can often provide a more performance-efficient solution.

Nevertheless, changing collations may not always be practical especially in scenarios like legacy systems or shared environments. It's crucial to understand the capabilities and restrictions of your specific DBMS.

Alternate-case special characters

For characters beyond the English alphabet, case conversions through LOWER or UPPER might lead to unexpected results. In such situations, exploring Unicode and locale-specific behaviors is highly recommended.

Advanced techniques, tips, and tricks

Unleashing the power of wildcards

The wildcards in LIKE queries, % and _ can pack a punch when combined with case-insensitive search, creating more robust search patterns.

LIKE could sometimes lead to false negatives owing to invisible characters or unseen trailing spaces. Here, TRIM function can come handy to clean up before the comparison:

SELECT * FROM your_table WHERE LOWER(TRIM(column_name)) LIKE LOWER(TRIM('%search_value%')); -- Trim those spaces, like a good beard

The accent question

Accents in datasets could make case insensitivity more complex. This is where collations with accent sensitivity settings can help. Some systems support utf8_general_ci which is both accent and case insensitive.