I have a column in my table that I need to change the data type from VARCHAR
to DOUBLE
. Now I cannot convert it to DOUBLE
data type easily because the values on that column have numbers and strings.
> 0 41 14:16:57
ALTER TABLE `ControlAutomation`.`constantcomponentstabular`
DROP COLUMN `String`,
CHANGE COLUMN `ConstantValue` `ConstantValue` DOUBLE NULL DEFAULT NULL
> Error Code: 1265. Data truncated for column 'ConstantValue' at row 217966 1.891 sec
I know what is causing this issue, its because there are strings on the column that I want to change to DOUBLE
.
Here is the solution that I have in mind using Python
- Query the table.
- Check whether the value can be converted to
float
. - If not,
UPDATE
value of that column to0
.
This process takes a long time, My question is how to best change those values to any number so I will be able to ALTER the table.
I am using MySQL 5.6.32
and InnoDB
as storage engine.
Best Answer
Let me suggest to add a new column and then update this new column before to drop the old one.
ConstantValue
column.dbfiddle here