Fix 00-00-0000 00:00:00 date errors in MySQL

I want my updated_at fields to be:

  • non-null
  • equal to created_at when the record is created
  • updated by the db when the record changes

Sometimes I stumble on a project where the date field is allowed to be a zero date.

To fix that, I use the following query:

UPDATE users
SET updated_at = created_at
WHERE CAST(updated_at AS CHAR(20)) = '0000-00-00 00:00:00'

Then, I fix the field itself.

ALTER TABLE users CHANGE updated_at 
updated_at TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP

Click Here to Leave a Comment Below

Leave a Reply: