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.
- first cleanse column removing characters aren't numbers using
replace() - then take several parts of string separate them out using
substr() - 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
Post a Comment