How can I search (case-insensitive) in a column using LIKE wildcard?
Execute a case-insensitive LIKE
query by morphing both the column and the search item to lowercase using LOWER()
or uppercase using UPPER()
:
In SQL Server, we apply case-insensitive collation to our operations:
Enhancing search flexibility
To ensure case-insensitive matching, the LOWER()
function is your good friend. Combining LOWER()
with the LIKE
operator and the %
wildcard gives you the edge in solving your problems. Helper functions such as LOWER()
or LCASE()
expands SQL search flexibility:
The above SQL statement ensures that 'WIND', 'Wind', or 'wind' will all match the pattern %wind%
.
Collation in action
An interesting way to ensure case-insensitive search is to adjust column's collation setting. Here, UTF8_GENERAL_CI
enables case-insensitive operation:
The _ci
in SQL collations stands for case-insensitivity. Remember that changing a column's collation might trigger the need for rebuilding indexes on that column.
Per-query case sensitivity tweaks
On-the-fly changes to case sensitivity can be achieved through query-specific collation adjustments, leaving the column's main structure intact. This is the SQL equivalent of a "temporary tattoo":
The performance angle
Applying functions like LOWER()
and COLLATE
comes with performance considerations. For larger datasets or indexed columns, creating indexed computed columns with lowercase values is an efficient tactic. Binary collations can expedite case-insensitive searches where the language allows. It's the SQL equivalent of "hitting two birds with one stone":
Scaling code readability
For prolonged maintenance and easy perusal, subquerying and utilizing aliases can keep your SQL scripts organized:
Was this article helpful?