- Home
- .NET tutorials
- Which date and time type to use in SQL Server 2019?
Which date and time type to use in SQL Server 2019?
Published: Tuesday 30 June 2020
Dates & Times. Can become very simple if you are using to store dates of events for example. However, it can get more complicated when you involve the time, particularly when it comes to time zones.
The Different Date & Time Types
Here are a current list of date & time types available in SQL Server 2019:
- date
- datetime
- datetime2
- datetimeoffset
- smalldatetime
- time
Now, there is a full documentation available for these in the SQL documentation that Microsoft provide. But, we are going to find scenarios where you might use each one.
The 'date' Type
As you can guess, the date type only stores the date and doesn't contain anything about the time of day. The range goes from 1st January, 0001 to 31st December, 9999.
The 'datetime' and 'datetime2' Types
Both of these types allow you to store the time. But there are differences between the two of them.
The 'datetimeoffset' Type
If you are in a country that doesn't observe daylight savings, then you may be able to disregard this type. However, if you are, or you are building an application where you need the ability to convert a time to a particular time zone, then this type will be for you.
2020-04-27 12:00 +01:00
This date is storing that it's 27th April 2020 at 12:00. It's also stating that the time stored is one hour ahead of UTC.
The 'smalldatetime' Type
It's called small for a reason. The range only goes from 1st January, 1900 to 6th June 2079. You cannot store fractional seconds with it. However, with these limitations come benefits. It only has 4 bytes of storage, half of datetime2 and datetimeoffset, if you take up the full precision.
The 'time' Type
If you are not interested in storing the date, or wish to separate the date and time into separate columns, then you can use the time type. This will store the time up to seven fractional seconds.
Storage vs. Correct Data
It's easy to get caught up in the amount of storage that each type takes up. And if you are fielding a system that has millions of records, then it's worth considering. However, I think it's important that you have the correct data stored. If you need the ability to convert to different time zones, then the datetimeoffset type is the one you should use. Even if don't need that functionality at this point, I don't think there's any harm in storing the information.
Latest tutorials
File logging in ASP.NET Core made easy with Serilog
Learn how to add file logging to an ASP.NET Core app using Serilog, including setup, configuration, and writing logs to daily rolling files.