Jump to content

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
https://forums.phpfreaks.com/topic/157797-sql-strip-non-numeric-characters/
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); 

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.