2

I am using the following query to select duplicate phone numbers from a table.

SELECT id, REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( phone, "+", '' ) , ")", '' ) , "(", '' ) , "-", '' ) , ' ', '' ) AS strippedPhone
FROM `customers`
GROUP BY strippedPhone
HAVING count( strippedPhone ) >1
LIMIT 0 , 300

It looks ugly and does not consider the possibilities of alphanumeric character on the field having the phone number.

Any better ways?

Starx
  • 77,474
  • 47
  • 185
  • 261

2 Answers2

0

This StackOverflow post MySQL strip non-numeric characters to compare has a solution that uses the NumericOnly function.

http://venerableagents.wordpress.com/2011/01/29/mysql-numeric-functions/

Community
  • 1
  • 1
Revent
  • 2,091
  • 2
  • 18
  • 33
  • I came across that solution. Does not look that good either. Replace is much faster that that I think. – Starx Mar 28 '13 at 00:42
0

In my opinion you would be to normalize the phone numbers before you stored them in the database. Any sql query that is able to compare phone numbers regardless of format is going to be highly inefficient. Where as if you if you reformat the phone numbers before inserting them into the database checking for duplicate numbers is trivial.

If you want to do this on an existing database you will have to normalize the data in the database first but that only has to be done once.

Aaron Levenstein
  • 385
  • 2
  • 12