Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use separate date and time instead of a single datetime

If I want to store date and time, is it better to store them in a separate date and time or use a single datetime?

When should we use date and time instead of a single datetime?

I want to filter my queries either using date or time.

like image 424
Harke Avatar asked Mar 19 '13 14:03

Harke


People also ask

Should I use date or datetime?

The DATE type is used for values with a date part but no time part. 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.

Is it better to use TIMESTAMP or datetime?

Timestamps are also lighter on the database and indexed faster. The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in YYYY-MM-DD HH:MM:SS format.

What is the difference between datetime and TIMESTAMP?

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. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.


2 Answers

When you are talking about a moment in time, whether a universal moment, or a specific date and time on someone's local calendar, you use a datetime. If you want to be sure that you are talking about an exact moment in time, regardless of the observer, then you use a datetimeoffset.

If you are storing just a date then you mean a date without a time component, meaning "any time on this date".

If you are storing just a time then you mean a time without a date component, meaning "this time on any date", or "this time on a date determined by some other means".

There is no practical purpouse to having both a date and a time that are about the same thing, sitting on the same row. Just use a datetime for that.

like image 156
Matt Johnson-Pint Avatar answered Sep 21 '22 12:09

Matt Johnson-Pint


In SQL Server 2008 you have date and time data types so this becomes a non issue.

If it is good choice it really depends by your business and how you will query you data.

If for example you want to know all the orders places between 1 and 2 PM for any day using a separated Date and Time column will make it quicker

like image 32
Massimiliano Peluso Avatar answered Sep 17 '22 12:09

Massimiliano Peluso