Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL regex query case insensitive

Tags:

mysql

In my table I have firstname and last name. Few names are upper case ( ABRAHAM ), few names are lower case (abraham), few names are character starting with ucword (Abraham).

So when i am doing the where condition using REGEXP '^[abc]', I am not getting proper records. How to change the names to lower case and use SELECT QUERY.

SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abc]';

This is my query, works fine if the records are lower case, but my records are intermediate ,my all cus name are not lower case , all the names are like ucword.

So for this above query am not getting proper records display.

like image 950
Bharanikumar Avatar asked Oct 22 '10 01:10

Bharanikumar


2 Answers

Have you tried:

SELECT * FROM `test_tbl` WHERE LOWER(cus_name) REGEXP '^[abc]';
like image 173
codaddict Avatar answered Oct 03 '22 00:10

codaddict


I think you should query your database making sure that the names are lowered, suppose that name is the name you whish to find out, and in your application you've lowered it like 'abraham', now your query should be like this:

SELECT * FROM `test_tbl` WHERE LOWER(cus_name) = name

Since i dont know what language you use, I've just placed name, but make sure that this is lowered and you should retrieve Abraham, ABRAHAM or any variation of the name!

Hepe it helps!

like image 45
David Conde Avatar answered Oct 03 '22 01:10

David Conde