Changing the data type of a sql column

7

Good morning.

I have the CliTelCel char(10) column in the Clientes table, I need to change char(10) to char(11) . But this column already exists data, what would be the best way to proceed in this case, without losing the existing data and make the appropriate change?

Thank you.

    
asked by anonymous 18.05.2017 / 14:47

3 answers

6

First of all, have a backup, this is the guarantee that nothing will be lost.

To change the type or size:

ALTER TABLE Clientes ALTER COLUMN CliTelCel char(11)

Data will not be lost because you just increased the size of the column.

If you prefer you can also enter some data to complete the column (ex: 0)

Update  Clientes set CliTelCel= '0'+CliTelCel
    
18.05.2017 / 14:56
3
alter table Clientes alter column CliTelCel char(11)
    
18.05.2017 / 14:55
3

Considering that SGBD is SQL Server you can change the type directly:

ALTER TABLE [tabela] ALTER COLUMN [coluna] char(11);
    
18.05.2017 / 14:55