Jump to content

SQL - Strip non-numeric characters


mystifier

Recommended Posts

The following code is used to strip non-numeric characters from a very large table:

 

$prep1 = @mysql_query("update contacts set phone =  replace(phone, ' ', '')");

$prep2 = @mysql_query("update contacts set phone =  replace(phone, '-', '')");

$prep3 = @mysql_query("update contacts set phone =  replace(phone, '(', '')");

$prep4 = @mysql_query("update contacts set phone =  replace(phone, '), '')");

 

Can this be done in a single query?

 

 

Link to comment
Share on other sites

i think you can use a single UPDATE query, with a CASE/WHEN/THEN statement (for each char), and the REGEXP operator:

UPDATE contacts 
SET phone = ( 
CASE  
WHEN phone REGEXP '.*-.*'
THEN replace(phone,'-','') 
ELSE phone 
WHEN [...]
THEN [...]
ELSE [...]
END); 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.