MySQL column: Change existing phone numbers into specific format? -


i have mysql column contains phone numbers, problem they're in different formats, such as:

  • 2125551212
  • 212-555-1212
  • (212)5551212

i'd know if it's possible take existing 10 digits, remove formatting, , change them format: (212) 555-1212

not duplicate, i'm looking update several thousand entries instead of masking new entries.

unfortunately, no regexp_matches() or translate() function comes standard mysql installation (they postgres), way find dirty, works.

  1. first cleanse column removing characters aren't numbers using replace()
  2. then take several parts of string separate them out using substr()
  3. finally, concatenate them adding symbols between substrings concat()

if have more characters need truncate, add replace() on top of 3 existing.

sample data

create table nums ( num text );  insert nums values      ('2125551212'),     ('212-555-1212'),     ('(212)5551212'); 

query formatting data

select    num,    concat('(',substr(num_cleansed,1,3),') ',substr(num_cleansed,4,3),'-',substr(num_cleansed,7)) num_formatted (   select      num,      replace(replace(replace(num,'(',''),')',''),'-','') num_cleansed   nums   ) foo 

result

num             num_formatted 2125551212      (212) 555-1212 212-555-1212    (212) 555-1212 (212)5551212    (212) 555-1212 

click here sqlfiddle preview output.

i'm leaving update statement homework reader.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -