Mysql – “1292 Incorrect datetime value” when updating an “empty” datetime field

datetimeMySQLupdate

I have an updated_at field that was not initially set with default datetime value on the server. For a short period, a bunch of records got inserted with an "empty" value and are showing up at 0000-00-00 00:00:00 in the updated_at column. I wrote am update command to copy the values of the created_at field to updated_at and it is updating the field correctly, however, when I run it in MySQL Workbench, I get the following warning:

6 row(s) affected, 1 warning(s): 1292 Incorrect datetime value: '' Rows matched: 6  Changed: 6  Warnings: 1

My query:

UPDATE mytable
SET updated_at=created_at
WHERE updated_at='' and id >0;

I'm not sure where that warning is coming from since the created_at times are all valid and the updated_at fields are being set with the correct data.

Best Answer

Congratulation on your commitment to clean dates.

Temporary disabling the NO_ZERO_DATE sql_mode will enable this cleanup.

Just to be sure, use 0000-00-00 00:00:00 as the date too.

SET @@SQL_MODE = REPLACE(@@SQL_MODE, 'NO_ZERO_DATE', '');
UPDATE mytable
SET updated_at=created_at
WHERE (updated_at='' OR updated_at='0000-00-00 00:00:00') and id >0;