Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing 'invalid' date in a database

Tags:

database

mysql

I am storing some records in a database. A record has a 'last visited' field which is a timestamp. If a record has not been visited yet, the timestamp is invalid. I am currently storing a future date e.g. '2101-01-01 00:00:00' in the 'last visited' field, to denote an invalid date.

Is there a better way to indicate an 'invalid' date. What is the recommended 'best practise' for doing this?

I am using MySQL, but ideally, the recommendation should be db agnostic

like image 877
skyeagle Avatar asked Dec 08 '10 00:12

skyeagle


People also ask

How should dates be stored in a database?

Rule #1 - STORE DATETIMES IN UTC IN YOUR DATABASE, AND BACK END CODE. It is important that there is consistency across all your date-related data. When storing dates in the database, they should always be in UTC.

What does invalid date format mean?

Check the content of the spreadsheet you are trying to upload. Dates in your spreadsheet may be formatted in ways other than mmm-yyyy, such as dd-mmm-yyyy. Right-click these dates and click Format. Under Number, set a format of mmm-yyyy.

How do I convert invalid date to valid date in Excel?

A lot of users select the entire range and then go to Format Cell and then change the format of the Date. But it will not change anything as it is not a valid date format in Excel since dot separators are invalid. If it is not a date, then it's simple text to excel.

How do I fix invalid date in Google Sheets?

We recommend setting your Google Sheets cells as "date" format to ensure date range compatibility. If you're getting an invalid date format on your widget, please format your date column by selecting the column > Format > Number > Date.


3 Answers

Store a NULL value instead. MySQL timestamps are pretty screwed up, so you might need to change your table schema to encourage it to let you put a NULL in there; I use DATETIME instead, it's a little less weird than TIMESTAMPs in MySQL.

like image 164
El Yobo Avatar answered Oct 30 '22 04:10

El Yobo


The appropriate method of storing something of no value is to simply provide no value. That is to say, a null. Storing anything else and treating it as a magical value is often problematic. There are times when the magical value can be a valid value, and now you have no method of distinguishing the two.

like image 20
Anthony Pegram Avatar answered Oct 30 '22 03:10

Anthony Pegram


I would just leave the field null.

If they've never visited the record, it shouldn't have a timestamp at all.

like image 1
Brandon Avatar answered Oct 30 '22 03:10

Brandon