Search code examples
mysqldatefield

Mysql Date Default insert in 1970-01-01


I am inserting an empty date-field, but a default value 1970-01-01 gets inserted automatically. Why?

I even changed the Date structure to allow null, set Default as Null, still it's inserting 1970-01-01.


Solution

  • You are most likely inserting an empty string. Inserting an entry string will result in 1970-01-01 being written instead. You have to really insert NULL if you don't want this to happen.

    What you want to do is:

    INSERT INTO table
    SET datefield = NULL
    

    Not "NULL" but NULL Otherwise it will read it as a string and not as NULL

    You can also just don't set the column at all in your insert and than it will use NULL as well