Link to home
Start Free TrialLog in
Avatar of jb702
jb702

asked on

Storing Dates in Databases

Is there a standard for storing dates in databases. I've stored dates as formatted dates, numerical dates, string dates, etc. So I was just curious if one way is better than another way. I typically am only using vba and access.
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Databases will generally have one or more data types specifically geared for dates and/or times.  For example, in SQL Server 2005 and later you have:

datetime
datetime2
date
time

You are generally better off using one of those data types, and then if needed applying any formatting in your presentation layer.
There is no standard defined for the date storage because we even don't have any standard for date calculation over the world and we are still using several calendars...

The calendar standard is improving continuously and no database which could cover all these past and today's standards exist. And it would be difficult to create it because the calendar used by certain group of people depends on the region, politics, religion etc. You may see the list here: http://en.wikipedia.org/wiki/List_of_calendars

Thus if you are using MS Access then store date into the DateTime field if it covers your needs. Once you decide to use some feature which is not implemented in Access dates (e.g. approximate date etc.) then you have to introduce your own date format (or look around for a solution).

Advantages of existing Date and DateTime data types:
- provide basic calculations and set of date functions
- validate the data entered by users

Disadvantages
- restricted date range and/or time accuracy
- no globally accepted standard is defined
ASKER CERTIFIED SOLUTION
Avatar of awking00
awking00
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You probably want to use ANSI SQL:2008 DATETIME (you know it is standard of SQL)
Avatar of Member_2_276102
Member_2_276102

I've stored dates as formatted dates, numerical dates, string dates, etc. So I was just curious if one way is better than another way.

In almost all databases, it's better to store as whatever DATE or DATETIME data type is made available by the DBMS. In that sense, none of the ones you listed is a "better way". In most cases, none of them are even "good" ways, though there can be reasons to do it with those.

As noted already, the various 'Date/Time' functions are not available without conversion if some numeric or character data type is used. However, if those functions aren't needed, there can sometimes be little reason to be concerned about it.

'Date' values can be looked at in two kinds of ways.

First, they can need to be handled as "dates", where aspects such as durations or positions in a calendar (day of week, week of year, etc.) are important. If date manipulations are important, then a true DATE data type is called for. You shouldn't need to convert the format to run the functions, nor should there be application code that has to do the work of built-in functions.

But if it's just a kind of identifying element, such as a marker for the creation of a log file or marking the date when some event happened, then it's little more than a tag value. There's often no automating value other than for a sort sequence. In that kind of case, then the better choice can often be whatever the DBMS has that's most efficient for the usage. This type of data is little more than a sequential identifier.

Deciding what is better for any specific database table, though, is difficult. Unless there is a known reason not to, the DBMS-supplied DATE or DATETIME data type should be used.

Tom
only standard types are datetime and interval