Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I looked around some and didn't find what I was after so here goes.

SELECT * FROM trees WHERE trees.`title` LIKE  '%elm%' 

This works fine, but not if the tree is named Elm or ELM etc...

How do I make SQL case insensitive for this wild-card search?

I'm using MySQL 5 and Apache.

like image 624
David Morrow Avatar asked May 20 '10 18:05

David Morrow


People also ask

Is like wildcard case-sensitive?

LIKE performs case-insensitive substring matches if the collation for the expression and pattern is case-insensitive. For case-sensitive matches, declare either argument to use a binary collation using COLLATE , or coerce either of them to a BINARY string using CAST .

How can I search without case-sensitive in SQL?

Case insensitive SQL SELECT: Use upper or lower functions or this: select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.

How do I ignore case-sensitive in SQL LIKE operator?

Using LOWER( ad UPPER() functions for case sensitive queries In the similar fashion UPPER() and LOWER() functions can be used in the LIKE clause for getting similar records and making the search insensitive in the table or database having collation that shows CS that is case sensitive in its collation.

How do I create a like case-insensitive query in MySQL?

select * from users where lower(first_name) = 'ajay'; The method is to make the field you are searching as uppercase or lowercase then also make the search string uppercase or lowercase as per the SQL function.


2 Answers

I've always solved this using lower:

SELECT * FROM trees WHERE LOWER( trees.title ) LIKE  '%elm%' 
like image 114
cwallenpoole Avatar answered Sep 26 '22 00:09

cwallenpoole


SELECT  * FROM    trees WHERE   trees.`title` COLLATE UTF8_GENERAL_CI LIKE '%elm%' 

Actually, if you add COLLATE UTF8_GENERAL_CI to your column's definition, you can just omit all these tricks: it will work automatically.

ALTER TABLE trees   MODIFY COLUMN title VARCHAR(…) CHARACTER   SET UTF8 COLLATE UTF8_GENERAL_CI.  

This will also rebuild any indexes on this column so that they could be used for the queries without leading '%'

like image 43
Quassnoi Avatar answered Sep 23 '22 00:09

Quassnoi