How to store NULL values in datetime fields in MySQL?

Viewed 193645

I have a "bill_date" field that I want to be blank (NULL) until it's been billed, at which point the date will be entered.

I see that MySQL does not like NULL values in datetime fields. Do any of you have a simple way to handle this, or am I forced to use the min date as a "NULL equivalent" and then check for that date?

Thanks.

EDITED TO ADD:

Ok I do see that MySQL will accept the NULL value, but it won't accept it as a database update if I'm updating the record using PHP.

The variable name is $bill_date but it won't leave the variable as NULL if I update a record without sending a value to $bill_date -- I get this error:

Database query failed: Incorrect datetime value: '' for column 'bill_date' at row 1

I assume I need to actually send the word NULL, or leave it out of the update query altogether, to avoid this error? Am I right? Thanks!!!

9 Answers

If your datetime column is not accepting null values it might be not nullable, so you can update the column using this command:

ALTER TABLE your_table modify your_coloumn datetime null

And when you want to save a null field, you can leave it alone (not assigning anything to that field), or you can set it to Null (instead of '')

Related