Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best field to store the birthday?

Tags:

mysql

I have to store in my mysql database, the users's birthday ( like 12-11-1990 ).

What field type do I have to use to store it ?

like image 972
xRobot Avatar asked Apr 30 '12 19:04

xRobot


People also ask

Which data type should be used to store date of birth?

Answer. DATETIMEto store a date, you should probably use a date. to quote the documentation: The DATE type is used for values with a date part but no time part. If you use this last method, I'd advise you to also store a DATETIME version of the birthday so that you can build the date without any kind of maneuvers.

What is data type of DOB in MySQL?

MySQL retrieves and displays DATE values in ' YYYY-MM-DD ' format. The supported range is '1000-01-01' to '9999-12-31' . The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format.

How can get date in dd mm yyyy format in MySQL?

MySQL DATE_FORMAT() Function The DATE_FORMAT() function formats a date as specified.


2 Answers

to store a date, you should probably use a date. to quote the documentation:

The DATE type is used for values with a date part but no time part.

like image 149
oezi Avatar answered Oct 12 '22 03:10

oezi


You can store it as a DATE object as you would normally do with non-repeating dates. The, if you're using MySQL (I believe other DBMS also have functions for this) you can retrieve birthdays using MySQL functions as described here:

SELECT *  FROM table_name  WHERE      MONTH(date_field) = desired_month AND      DAY(date_field) = desired_day 

This method can be a bit slow when dealing with thousands of records. If that's your case, you can store the birthday with 3 separate INTs, one for each (year, month, day). Then you search birthdays like this:

SELECT * FROM table_name WHERE    month_field = desired_month AND    day_field = desired_day 

If you use this last method, I'd advise you to also store a DATETIME version of the birthday so that you can build the date without any kind of maneuvers. You can additionally index each one of the 3 fields (non-unique) so it's faster to retrieve them.

like image 37
Tiago Avatar answered Oct 12 '22 03:10

Tiago