Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL search to ignore hyphens

Tags:

mysql

How can I search for "1-800-flowers" by "1800flowers" in MySQL?

I have the data "1-800-flowers", but I want to find it by "1800flowers".

like image 261
Mahfuz Avatar asked Jan 21 '23 13:01

Mahfuz


2 Answers

You're probably best off creating a second column that you fill with 1800flowers (replacing all characters you want to ignore) and searching that. That way, you can make full use of indexing.

A quick way to convert all existing data would be

UPDATE table SET columnname_without_hyphens = REPLACE(columnname, "-", "");
like image 183
Pekka Avatar answered Jan 31 '23 09:01

Pekka


If your problem is just ignoring hyphens, I may suggest using REPLACE to eliminate them, like this:

SELECT ... WHERE REPLACE(column, '-', '') ...

Otherwise, if you're looking for strings that "sound alike", you may want to have a look at the SOUNDEX function.

like image 45
ChrisJ Avatar answered Jan 31 '23 11:01

ChrisJ