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.
The case sensitivity is defined in the columns / tables / database collation settings. You can do the query under a specific collation in the following way:
for instance.
(Replace
utf8_general_ci
with whatever collation you find useful). The_ci
stands for case insensitive.When I want to develop insensitive case searchs, I always convert every string to lower case before do comparasion
Actually, if you add
COLLATE UTF8_GENERAL_CI
to your column's definition, you can just omit all these tricks: it will work automatically.This will also rebuild any indexes on this column so that they could be used for the queries without leading '%'
Simply use :
Or Use
Both functions works same
I'm doing something like that.
Getting the values in lowercase and MySQL does the rest
And For MySQL PDO Alternative:
You don't need to
ALTER
any table. Just use the following queries, prior to the actualSELECT
query that you want to use the wildcard: