SQL
case-insensitive search
LIKE wildcard
SQL queries
database search

How can I search case-insensitive in a column using LIKE wildcard?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Searching within a database to identify records that match a particular pattern is a common task for database administrators and developers. This task can be optimized by using the LIKE wildcard operator in SQL. One challenge is ensuring the search is case-insensitive, as databases may differentiate between uppercase and lowercase text depending on their collation settings. Below, we'll delve into how to conduct a case-insensitive search using the LIKE wildcard.

Understanding SQL LIKE Wildcard

The SQL LIKE operator is used for pattern matching with strings. It allows for partial matches to search for entries within a database. The two most common wildcards used in conjunction with LIKE are:

  • %: Represents zero or more characters.
  • _: Represents a single character.

Case Insensitivity in LIKE Searches

By default, the behavior of the LIKE operator in terms of case sensitivity depends on the database system’s collation setting. Generally:

  • Collation: It defines how string comparison is handled within a database. Case insensitivity is often controlled by using a case-insensitive collation.

Making LIKE Case Insensitive

  1. Database Collation: Ensure the column's collation is set to a case-insensitive collation.
    • Example in MySQL:
sql
     SELECT column_name FROM table_name
     WHERE column_name LIKE 'pattern%' COLLATE utf8_general_ci;

In this sample SQL query, utf8_general_ci is a case-insensitive collation ensuring the search is not case-sensitive.

  1. Upper/Lower Functions: Convert the column and the pattern to a common case using functions like UPPER() or LOWER().
    • Example:
sql
     SELECT column_name FROM table_name
     WHERE UPPER(column_name) LIKE UPPER('pattern%');

This method converts both the column and the search pattern to uppercase, ensuring case insensitivity.

Examples in Different SQL Dialects

MySQL

MySQL is usually case-insensitive by default if utf8_general_ci is set, but for explicit case insensitivity:

sql
SELECT name FROM users
WHERE name LIKE 'john%';

This query will match "John", "john", "JOHN", etc., if the column uses a case-insensitive collation.

PostgreSQL

In PostgreSQL, the concept of ILIKE comes handy for case-insensitive pattern matching:

sql
SELECT name FROM users
WHERE name ILIKE 'john%';

SQL Server

SQL Server also manages case sensitivity through collation. You can specify a different collation in the query to perform a case-insensitive search:

sql
SELECT name FROM users
WHERE name LIKE 'john%' COLLATE Latin1_General_CI_AS;

Evaluating Performance

Case-insensitive searches can sometimes impact performance. A recommended practice is to:

  • Use Indexed Searches: If your columns are indexed, ensure the index supports the collation used in the search.
  • Consider Database Design: Store data in a consistent case (either all caps or all lower) and perform comparisons in the same case.

Key Points Summary

ConceptDetails
LIKE OperatorUsed for pattern matching in string searches using % and _ wildcards.
Case SensitivityDetermined by database collation; can be changed or forced using collation settings or case conversion functions.
MySQLUses collation to define case sensitivity; can apply COLLATE keyword in the query.
PostgreSQLProvides ILIKE for simple case-insensitive searches.
SQL ServerRelies on collation; specify COLLATE within your query to override default settings temporarily.
Best PracticesSet consistent case storage, use indexed searches, ensure collation compatibility, and consider performance impacts of case-insensitive operations.

Conclusion

Conducting a case-insensitive search using the LIKE wildcard operator can be efficiently managed by understanding your database's collation settings and leveraging functions or operators specific to your SQL dialect. Careful consideration and knowledge of these mechanisms are crucial for maintaining efficient and accurate data queries.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.