In this article, we will compare DATETIME2 vs DATETIME in SQL Server, discuss about their precision and see some examples.
About DATETIME2 in SQL Server
The DATETIME2 is a data type in SQL Server, that was originally included in SQL Server 2008.
We all know of the existence of the DATETIME.
So, what is the difference between these two data types?
Let’s try to compare them with some examples.
Comparison 1: Notice the date – Both data types work fine
select cast('1753-01-01 18:00:00.123' as DATETIME) as [datetime];
Result (Success): 1753-01-01 18:00:00.123
select cast('1753-01-01 18:00:00.123' as DATETIME2) as [datetime2];
Result (Success): 1753-01-01 18:00:00.1230000
Comments: Please note the precision of the DATETIME2. It provides support up to nanoseconds!
Comparison 2: Notice the time precision
select cast('1753-01-01 18:00:00.1234' as DATETIME) as [datetime];
Result (Error): Conversion failed when converting date and/or time from character string
select cast('1753-01-01 18:00:00.1234' as DATETIME2) as [datetime2];
Result (Success): 1753-01-01 18:00:00.1234000
Comments: DATΕTIME does not support time precision more than milliseconds and that’s why the above conversion fails. Though, the DATETIME2 supports up to nanoseconds and the conversion works.
Comparison 3: Notice the date values
select cast('1653-01-01 18:00:00.123' as DATETIME) as [datetime];
Result (Error): The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
select cast('1653-01-01 18:00:00.123' as DATETIME2) as [datetime];
Result (Success): 1653-01-01 18:00:00.1230000
Comments: DATΕTIME does not support date values before the year 1753 and that’s why the conversion fails. Though, DATETIME2 supports until back to year 0001 and so the conversion works.
Concluding Remarks
The DATETIME2 offers support for larger date ranges and larger time precision.
DATETIME: Date and Time Ranges
- The supported date range is:1753-01-01 through 9999-12-31 (January 1, 1753, AD through December 31, 9999 AD)
- The supported time range is: 00:00:00 through 23:59:59.997
DATETIME2: Date and Time Ranges
- The supported date range is: 0001-01-01 through 9999-12-31 (January 1,1 AD through December 31, 9999 AD)
- The Supported time range is: 00:00:00 through 23:59:59.9999999
Strengthen you SQL Server Development Skills – Enroll to our Online Course!
Check our online course titled “Essential SQL Server Development Tips for SQL Developers” (special limited-time discount included in link).
Via the course, you will sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!
Did you find this article useful and interesting? Find hundreds of useful SQL Server programming/development articles in my eBook: “Developing with SQL Server (Second Edition“.
Check our other related SQL Server Development articles.
Check out our latest software releases!
Subscribe to our newsletter and stay up to date!
Featured Online Courses:
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- The Philosophy and Fundamentals of Computer Programming
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- Introduction to Azure SQL Database for Beginners
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- How to Import and Export Data in SQL Server Databases
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
Check our Related SQL Server Development Articles:
- Essential SQL Server Development Tips for SQL Developers
- How to Import and Export Data in SQL Server
- Listing all Tables of a Linked Server’s Database
- How to Import and Export Unstructured Data in SQL Server – FileTables
- How to Import and Export Unstructured Data in SQL Server – FILESTREAM
- How to Import and Export Unstructured Data in SQL Server – The IMAGE Datatype
- …more
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.
It is very nice and easily understandably, Thanks