Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-insensitive REPLACE in MySQL?

Tags:

replace

mysql

MySQL runs pretty much all string comparisons under the default collation... except the REPLACE command. I have a case-insensitive collation and need to run a case-insensitive REPLACE. Is there any way to force REPLACE to use the current collation rather than always doing case-sensitive comparisons? I'm willing to upgrade my MySQL (currently running 5.1) to get added functionality...

mysql> charset utf8 collation utf8_unicode_ci; Charset changed  mysql> select 'abc' like '%B%'; +------------------+ | 'abc' like '%B%' | +------------------+ |                1 | +------------------+  mysql> select replace('aAbBcC', 'a', 'f'); +-----------------------------+ | replace('aAbBcC', 'a', 'f') | +-----------------------------+ | fAbBcC                      |   <--- *NOT* 'ffbBcC' +-----------------------------+ 
like image 287
dkarp Avatar asked Apr 13 '11 21:04

dkarp


People also ask

Is MySQL replace case-sensitive?

Definition and Usage The REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: This function performs a case-sensitive replacement.

Is replace case-sensitive in SQL?

Note: The REPLACE function is case-insensitive in MS SQL Server.

What is case-insensitive in MySQL?

Table names are stored in lowercase on disk and name comparisons are not case-sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.

How would you make a 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.


1 Answers

If replace(lower()) doesn't work, you'll need to create another function.

like image 198
fvox Avatar answered Sep 20 '22 17:09

fvox