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.
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
- Database Collation: Ensure the column's collation is set to a case-insensitive collation.
- Example in MySQL:
In this sample SQL query, utf8_general_ci is a case-insensitive collation ensuring the search is not case-sensitive.
- Upper/Lower Functions: Convert the column and the pattern to a common case using functions like
UPPER()orLOWER().- Example:
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:
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 Server
SQL Server also manages case sensitivity through collation. You can specify a different collation in the query to perform a case-insensitive search:
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
| Concept | Details |
| LIKE Operator | Used for pattern matching in string searches using % and _ wildcards. |
| Case Sensitivity | Determined by database collation; can be changed or forced using collation settings or case conversion functions. |
| MySQL | Uses collation to define case sensitivity; can apply COLLATE keyword in the query. |
| PostgreSQL | Provides ILIKE for simple case-insensitive searches. |
| SQL Server | Relies on collation; specify COLLATE within your query to override default settings temporarily. |
| Best Practices | Set 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
- How can I see the raw SQL queries Django is running?
- How can I see the specific value of the sql_mode?
- How can I SELECT rows with MAX(Column value), PARTITION by another column in MYSQL?
- How can I SELECT rows with MAXColumn value, PARTITION by another column in MYSQL?
- How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?
- How can I show a end-to-end transaction over RabbitMQ in Application Insights?
- How can I solve a connection pool problem between ASP.NET and SQL Server?
- How can I solve Error MySQL shutdown unexpectedly?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.