Changing the data type of an sql column

Asked

Viewed 13,782 times

8

Good morning.

I have the spine CliTelCel char(10) on the table of Clientes, need to change char(10) for char(11). However 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.

  • 3

    What database are you using?

  • char or varnish?

  • @Wendelrodrigues am using Microsoft SQL Server Management Studio 2014

  • @Reginaldorigo para char(11)

3 answers

7


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 as you just increased column size.

You can also enter some data to complete the column (e.g.:0)

Update  Clientes set CliTelCel= '0'+CliTelCel

3

alter table Clientes alter column CliTelCel char(11)

3

whereas the SGBD be it SQL Server you can change the type directly:

ALTER TABLE [tabela] ALTER COLUMN [coluna] char(11);
  • But what about the data that already exists there? It will not be affected?

  • No, the data is still there. The only problem would be if you deleted the column

Browser other questions tagged

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