Tony Stark
Tony Stark

Reputation: 25558

Does changing the datatype of a column after data is already there destroy the data?

Let's say I have a column of varchar(40) with data already and i change the datatype of that column to integer. does the data change at all in the columns (ie, does the data 'corrupt') or does it not matter and a table of (1,2,3) will still be (1,2,3) regardless of the datatype?

Upvotes: 1

Views: 1502

Answers (3)

HLGEM
HLGEM

Reputation: 96600

Your first problem when you do something like this is that not all the data may meet the criteria to make the change. Those records need to be found and fixed before changing a data type.

The safest way to do something like this is to:

Make a backup Find and fix any data that will not meet the criteria for the new datatype Create an additional column in the correct datatype Migrate the data Drop the orginal column Rename the new column to the old name

Upvotes: 0

womp
womp

Reputation: 116987

If you attempt to change a column's datatype and the new type is incompatible with the old type, it will fail and nothing will change. You'll get an error like:

"Disallowed implicit conversion from data type <type> to data type <type2>".

Upvotes: 2

gbn
gbn

Reputation: 432401

It will be the same, subject to

  • datatype conversion error (eg "foo" to int)
  • truncation (eg "foobar" in char(4))

Upvotes: 2

Related Questions