Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL column: Change existing phone numbers into specific format?

I have a MySQL column that contains phone numbers, the problem is that they're in different formats, such as:

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

I'd like to know if it's possible to take the existing 10 digits, remove the formatting, and change them all to this format: (212) 555-1212

Not a duplicate, as I'm looking to update several thousand entries instead of masking new entries.

like image 813
eclipsis Avatar asked Jan 05 '16 22:01

eclipsis


People also ask

How do I change the format of a phone number in SQL?

Using CONVERT - SELECT CONVERT( int, 5634.6334) as number. Using ROUND - SELECT ROUND(5634.6334,2) as number. Using CEILING - SELECT FLOOR(5634.6334) as number. Using FLOOR - SELECT CEILING(5634.6334) as number.

What format are phone numbers stored in database?

E. 164 is an international format which defines a general format for international telephone numbers. Based on this formating, the number contains a country code (CC), a national destination code (NDC), and a subscriber number (SN). There can be up to 15 digits in an E.

How do I concatenate a phone number with a country code in SQL?

Performance : 1. Use CONCAT instead of +; 2. Create a column called Country_ID in both tables and join on that. UI : use FORMAT function to make them look good and you can format them as per their country standards.

What data type should a phone number be in a database?

I generally use VARCHARs to store telephone numbers. Storage is not so expensive these days that I benefit that much from the savings found by storing them as numeric values.


1 Answers

Unfortunately, no REGEXP_MATCHES() or TRANSLATE() function comes with standard MySQL installation (they do with Postgres), so you could do this a way which I find really dirty, but it works.

  1. First you cleanse your column by removing characters that aren't numbers using replace()
  2. Then you take several parts of the string to separate them out using substr()
  3. Finally, you concatenate them adding symbols between your substrings with concat()

If you have any more characters that you need truncate, just add another replace() on top of 3 already existing.

Sample data

create table nums ( num text );

insert into nums values 
    ('2125551212'),
    ('212-555-1212'),
    ('(212)5551212');

Query formatting your data

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

Result

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

Click here SQLFiddle to preview output.

I'm leaving UPDATE statement as a homework for the reader.

like image 156
Kamil Gosciminski Avatar answered Nov 15 '22 05:11

Kamil Gosciminski