Answer the question
In order to leave comments, you need to log in
How to store incomplete date in Mysql?
Dear experts, please tell me how to properly store dates in the database, if the date may be incomplete. For example, a calendar of the release of something. We do not always know the exact release date, sometimes only the month and year, sometimes only the quarter and year, etc.
Here is an example of how to store:
The first line contains (month-year), the second line contains the exact date (day-month-year) , the third contains (quarter-year), and the last only (year).
How to store all this correctly and then work, sort and display it correctly? I use 4 different fields (date, month, quarter, year) but something tells me that there is a more logical and correct way.
Answer the question
In order to leave comments, you need to log in
The two fields are `date` and `estimation`. The second field is enum('date', 'month', 'quarter', 'year'). The first field contains the exact date or the last date of the corresponding interval. Then sorting ORDER BY (`date`, `estimation`) will return in the optimal order. Well, for beauty, it remains to convert the date to the appropriate interval.
Taking into account the essence of "quarter" - it is unlikely to turn out more logical, I do not remember a single ready-made date format / standard in the database, where there would be a "quarter" field, if only I made my own composite type, but there is no such functionality "out of the box" in MySQL and it's unlikely that it's easy to get it "screwed" in MySQL.
The only option that comes to my mind is to invent your own date format based on the CHAR / VARCHAR field, such as YEAR-QUARTER-MONTH-DAY, and empty values respectively. fill with zeros, so at least there will be some hint of the correct sorting.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question