Copy data from one column of a table to another of the same table

Asked

Viewed 5,838 times

2

Hey, guys, I have a question. I need to copy the data from an integer column to another decimal column of the same table, that in Mysql. Is it possible to do ? there may be some inconsistencies in the data? And how can I do?

  • 1

    In fact you don’t even need to copy, in these cases you can simply edit the table by changing the type. BUT, I always think it is good to copy and delete the previous one when it is to change type, to avoid labor repairing disasters ;)

  • Thanks guys. They helped me out. I think I’ll try to first change the field to decimal on a test basis and then go to the real bank.

  • You can answer your own question if you have found a different solution to the answers.

  • yes it is possible, if it were otherwise could have problems.

2 answers

3

I’m not sure I understand what you want to do, but it seems to me something very simple. Try to do the following, see if it helps you:

update tabela
set coluna_decimal = coluna_inteira 

2

Just change the type, it will solve the problem:

 ALTER TABLE `nome_da_tabela` 
 CHANGE `nome_da_coluna_inteiro` `nome_da_coluna_decimal` DECIMAL (6,2);

And to copy the data, you can make a select Insert:

SET SQL_SAFE_UPDATES = 0;

INSERT INTO tabela_destino
(campo1, campo2, campo3)
SELECT campo1, campo2, campo3 FROM tabela_origem;

Browser other questions tagged

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