Separate ddd from phone with SQL statement

Asked

Viewed 6,773 times

8

I have a table with fields DDD and Phone. Some were registered correctly, others the ddd is next to the phone and need to separate.

TABLE

+--------------------+
|   DDD  | Telefone  |  
+--------------------+     
|   47   | 89876543  | ---> ok
+--------------------+    
|  NULL  |4799843838 | ---> ajustar
+--------------------+    
|  NULL  |04799843838| ---> ajustar
+--------------------+    

How could I update my database using SQL statement to put the DDD in the correct field, because it is next to the phone, only it should enter the NULL value. Considering also that have to remove the 0 on the left (when)?

  • You will have to select, manipulate the strings of this select, see if the record is ok, if not, update the line separating the DDD from the phone, can you do it? If possible, pass your https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=active&q=Separate+column+in+2+Columns+sql

  • Thank you Paulo, but I don’t know much about programming and data manipulation. It is a simple table, which has an ID (int) and the DDD and Telephone columns are Varchar.

5 answers

4

You can remove all spaces and words NULL, if you don’t have a 0 left, add one, then format the string:

DECLARE @Telefones AS TABLE (
    [DDD Telefone] VARCHAR(50)
)

INSERT INTO @Telefones VALUES ('47 89876543')
INSERT INTO @Telefones VALUES ('NULL 4799843838')
INSERT INTO @Telefones VALUES ('NULL 04799843838');

WITH CTE_Telefones AS (
    SELECT REPLACE(REPLACE([DDD Telefone], 'NULL', ''), ' ', '') AS  [DDD Telefone]
    FROM @Telefones
), CTE_Telefones_Zero AS (
    SELECT 
        CAST(CASE CHARINDEX('0', [DDD Telefone])
            WHEN 1 THEN [DDD Telefone] 
            ELSE '0' + [DDD Telefone]
        END AS VARCHAR(MAX)) as [DDD Telefone]
    FROM CTE_Telefones
)

SELECT 
    '(' + SUBSTRING([DDD Telefone], 1, 3) + ') ' + 
    SUBSTRING([DDD Telefone], 3, LEN([DDD Telefone]) - 7) + '-' + 
    SUBSTRING([DDD Telefone], LEN([DDD Telefone]) - 3, 4) AS [DDD Telefone]
FROM CTE_Telefones_Zero

The SQL output will be something like:

(047) 7898-6543
(047) 7998-3838
(047) 7998-3838

3

You can be doing this way :

declare @contatos table
(
    ID_CONTATO_TELEFONE int,
    ID_CONTATO int,
    TIPO_TELEFONE varchar(1),
    DDD varchar(5),
    NUMERO varchar(100),
    IND_SITUACAO varchar(1),
    OBSERVACAO varchar(100)
)

insert into @contatos
values(1,1,'F','011','98876667','A',''),
(2,2,'F','','1198876667','A',''),
(3,3,'C','','01198876667','A',''),
(4,4,'C','','08798687667','A',''),
(5,5,'C','','(51)988765473','A',''),
(6,6,'C','','(051)98876543','A',''),
(7,7,'C','','(051)988765439 - ramal 3245','A',''),
(8,7,'C','','(051)88765439 - ramal 3245','A','');





select ID_CONTATO_TELEFONE,ID_CONTATO, TIPO_TELEFONE, 
case when DDD is not null and DDD <> '' then DDD 
     else case when SUBSTRING(NUMERO,1,2) = '(0' then SUBSTRING(NUMERO,2,3) 
               when SUBSTRING(NUMERO,1,1) = '(' then ('0' +SUBSTRING(NUMERO,2,2))
               when SUBSTRING(NUMERO,1,1) = '0' then SUBSTRING(NUMERO,1,3) 
               else  ('0' +SUBSTRING(NUMERO,1,2)) 
        end 
end as DDD,
case when DDD is not null and DDD <> '' then NUMERO
     else case when SUBSTRING(NUMERO,1,2) = '(0' then   case  when ISNUMERIC(SUBSTRING(NUMERO,6,9)) = 1 then SUBSTRING(NUMERO,6,9) else SUBSTRING(NUMERO,6,8) end
                   when SUBSTRING(NUMERO,1,1) = '(' then  case  when ISNUMERIC(SUBSTRING(NUMERO,5,9)) = 1 then SUBSTRING(NUMERO,5,9) else SUBSTRING(NUMERO,5,8) end
                   when  SUBSTRING(NUMERO,1,1) = 0 then  case  when ISNUMERIC(SUBSTRING(NUMERO,4,9)) = 1 then SUBSTRING(NUMERO,4,9) else SUBSTRING(NUMERO,4,8) end 
                   else NUMERO
            end  
end as NUMERO,
IND_SITUACAO, OBSERVACAO from @contatos;

Use the case and the SUBSTRING to remove the parts you need from the string.

inserir a descrição da imagem aqui

  • Thank you, for what I could see this is the solution I need, but it gave an error because in some numbers there is '(' or ')' as for example (51)98876543 or (051)98876543

  • @Brupikk, you can use the SUBSTRING(NUMERO,1,1) = '(' or SUBSTRING(NUMERO,1,2) = '(0' to verify this.

  • @Brupikk, I changed the answer..

  • Thanks Marconcilio! How do I ignore the record if it has letters in the middle and in this case leave as is? (some people put written Ramal or something else after the number, these we will ignore and leave as is, so is giving error because of the letters)

  • @Brupikk, this is gonna be a little tricky. a tip is to check if the phones are still 8 digits, if it is you can do the treatment to remove the DDD and then the next 8 characters or 9 in case and ignore the rest, the letter you speak always comes after the number?

  • Yeah, it’s complicated. heheh.. I talked to them, and then asked to only adjust if it’s in the mask (51)99999999 or (051)99999. If you are in one of these two ways, take the number ddd and put in the ddd field. Anything else leave as is

  • @Brupikk, look at the edition I’ve made, the part of it make the necessary change.

  • Thanks!! I will try

  • Got it!!! Thanks for the tips!!!!!!!!!

Show 4 more comments

1

select
 left( convert(bigint,replace( replace([Telefone],' ',''),'NULL','')),2) as DDD,
 Substring( convert(varchar(255),convert(bigint,replace( replace([Telefone],' ',''),'NULL',''))),3,11) as Telefone
 from Telefone
  • 1

    Could you explain why this works?

1

Basically you can use Substring:

UPDATE nome_tabela SET
   DDD = SUBSTRING(Telefone FROM 1 FOR 2)
  ,Telefone = SUBSTRING(Telefone FROM 3 FOR 9) 
 WHERE CAST(SUBSTRING(Telefone FROM 1 FOR 2) AS INTEGER) = 47;

Here testing starts with 0:

UPDATE nome_tabela SET
   DDD = SUBSTRING(Telefone FROM 2 FOR 3)
  ,Telefone = SUBSTRING(Telefone FROM 4 FOR 10) 
 WHERE CAST(SUBSTRING(Telefone FROM 1 FOR 1) AS INTEGER) = 0;

Do the tests, I’m without computer, answering by cell phone!

0

In my case I had 2 problems

  • Identify if it was a cell phone number
  • Refresh by typing 9.

Update Cell Phone Type

UPDATE Telefone SET descricao = "Celular" 
WHERE numero LIKE "(%" AND SUBSTRING(numero,5,1) IN ('6', '7', '8', '9');

Clear the formwork (old only)

UPDATE Telefone SET numero = REPLACE(REPLACE(REPLACE(numero,'(',''),'-',''),')','') 
WHERE descricao = "Celular" AND LENGTH(numero) = 13;

Formater adding 9 (old only - lenth = 10)

UPDATE Telefone SET numero = CONCAT('(',SUBSTRING(numero,1,2), ')9', SUBSTRING(numero, 3, 4), '-', SUBSTRING(numero, 7, 4)) 
WHERE descricao = "Celular" AND LENGTH(numero) = 10;

Currently by Anatel only phone numbers that start with 6,7,8,9 are mobile numbers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.