First of all that depends of course on the datatypes your DBMS supports. Does it have separate DATE
and TIME
types, is there a DATETIME
and maybe a timestamp, possibly with and without a timezone?
If I want to store a date (like DATE '2022-02-21'
), I'd use DATE
. If I want to store a date and time (like TIMESTAMP '2022-02-21 15:16:17'
), I would probably use DATETIME
. And only if I needed more precision (fractions of a second) or a timezone would I use a TIMESTAMP
.
By using the appropriate data type, I can use all date functions and date arithmetic available and have it guaranteed that no invalid data makes it into the database (such as February 30).
And then there are other cases. Say, I want to store birth dates including the birth time, but sometimes I don't know the time. Then I'd store date and time separately (in DATE
and TIME
columns) and make the date not nullable and the time nullable. Or if I want to store repeating calendar events (every February 3, every Monday and Tuesday, etc.), I will probably decide to have separate columns for year (INT
), month (INT
), day (INT
), weekday (INT
or VARCHAR
). And some matching check constraits of course.