John V
John V

Reputation: 5047

Is there a value for MySQL "null" date (0001-01-01)

In my DB, there is a non-nullable Date column. When I provide no date, the value reads 0001-01-01.

However, in the client, I need to know when the column is empty, and for that I use a silly condition:

if (Record.Year == 1)
{
   ///the column is empty, show "N/A" for date
}

Isn't there a more elegant way than this (the DB column cannot be changed)?

Upvotes: 1

Views: 1191

Answers (1)

Eugen Rieck
Eugen Rieck

Reputation: 65304

By chosing to define that column NOT NULL you told the database, that there always needs to be a (presumably valid) date there. If this doesn't fit your business logic, then you have only two possibilities:

  • Match business logic and DB model (ALTER TABLE tablename MODIFY COLUMN ...)
  • Live with this bad match: This will include crutches like "magic values" and it is very likely to lead to more trouble further down the road.

Upvotes: 4

Related Questions