30

I have a MySQL table with a VARCHAR(25) column that I want to convert to INT. All of the data in the column is either integer or blanks. I have tried this command:

ALTER TABLE ip MODIFY isp INT UNSIGNED NOT NULL;

Unfortunately, since some of the existing rows are empty I get this error:

ERROR 1366 (HY000): Incorrect integer value: '' for column 'isp' at row 2

Thanks for assistance.

1
  • You have existing records and no default value like 0, for example. It cannot put empty content for a number, said on short.
    – Rolice
    Feb 14, 2014 at 16:16

1 Answer 1

28

before altering your table, try to update your values.

The goal is to set a '0' value in the fields where you have empty values (which can't be converted to int)

update ip
set isp = '0' where trim(coalesce(isp, '')) = '';

If isp was not nullable, you can remove the coalesce function.

update ip 
set isp = '0' where trim(isp) = '';
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.