Formatting phones in the mysql table

0

I have a MYSQL table with the people record that the phones are formatted in this way.

519995150105
5133116284
51991178789
51999862438

What I need is a command that formats the phones to look like this:

(51)9995150105
(51)33116284
(51)991178789
(51)999862438
    
asked by anonymous 17.04.2018 / 20:23

2 answers

1

If the current formatting is always like you mentioned, it's simple. Just make a replace :

update tabela set telefone = replace(telefone,' 51',' (51)')

Only the first phone would be left unformatted, but then you can manually sort it or add another command.

    
17.04.2018 / 20:42
0

I use the following query to format my numbers. But mine are stored without the 55.
You can adapt your reality:

select 
  numero, 
  concat('(',substr(numero_cleansed,1,2),') ',substr(numero_cleansed,3,5),'-',substr(numero_cleansed,8)) AS numero_formatted
from (
  select 
    numero, 
    replace(replace(replace(numero,'(',''),')',''),'-','') as numero_cleansed
  from chip where LENGTH(numero)=11
  ) foo
    
17.04.2018 / 20:48