DATEDIFF() in SQL Server - Scaler Topics (2023)

Overview

The DATEDIFF function in SQL Server is used to calculate the difference between two dates. It has three parameters, interval, date1, and date2.

DATEDIFF returns an integer representing the difference between date1 and date2 with the unit specified by the interval. DATEDIFF function works in SQL Server (starting with 2008), Azure SQL Managed Instance, Azure SQL Database, Azure Synapse Analytic, and Analytics Platform System (PDW).

What is DATEDIFF() in SQL Server?

As the name suggests, the DATEDIFF function in SQL Server is used to calculate the difference between two dates.
DATEDIFF function works in SQL Server (starting with 2008), Azure SQL Managed Instance, Azure SQL Database, Azure Synapse Analytic, and Analytics Platform System (PDW).

Features Of DATEDIFF()

  • The DATEDIFF function in SQL Server is used to calculate the difference between two dates.
  • This function is classified as a Date Function.
  • This function accepts three parameters interval, date1, and date2.
  • This function can contain time in both the interval and the date value sections.

Syntax Of DATEDIFF() in SQL Server

Parameter of DATEDIFF() in SQL Server

DATEDIFF in SQL Server accepts three parameters,

Interval:

Interval is the part of the difference that has to be returned by the DATEDIFF function. It can be as given below:

ValueExplaination
year, yyyy, yySpecified Year
quarter, qq, qSpecified Quarter
month, mm, mSpecified month
dayofyearSpecified day of the year
day, dy, ySpecified day
week, ww, wkSpecified week
weekday, dw, wSpecified week day
hour, hhSpecified hour
minute, mi, nSpecified minute
second, ss, sSpecified second
millisecond, msSpecified millisecond

Let's understand how the interval is specified in the DATEDIFF function.

Let the dates be 2009/02/10 and 2015/02/10, and the interval be year. Then the DATEDIFF function will return 6, as the difference of year between the two dates is 6. The difference between the above dates is exactly 6 years, therefore if the month is passed as the interval, the DATEDIFF function will return 72 as 6 years contain 72 months.

Date1, Date2:

These are the dates from which the difference will be calculated by the DATEDIFF function.

Return Type Of DATEDIFF() In SQL Server

DATEDIFF in SQL Server returns an integer representing the difference between date1 and date2 with the unit specified by the interval.

If the returned result is out of range for 32-bit integer(-2,147,483,648 to +2,147,483,647), then DATEDIFF in SQL Server returns an error. Therefore in the above case, one should use the DATEDIFF_BIG() function.

Example of DATEDIFF() in SQL Server

Let's have some examples for the DATEDIFF function in SQL Server.
We will provide the dates to the function in the format YYYY/MM/DD HH:mm:ss.ms.

Passing Year as Interval

Output:

Passing Month as Interval

Output :

Passing Hour as Interval

Output:

Passing Second as interval

Output :

Conclusion

  • The DATEDIFF function in SQL Server is used to calculate the difference between two dates.
  • The DATEDIFF function accepts three parameters, interval, date1, and date2.
  • This function returns an integer representing the difference between date1 and date2 with the unit specified by the interval.
  • If the returned result is out of range for 32-bit integer (-2,147,483,648 to +2,147,483,647), then DATEDIFF in SQL Server returns an error.
Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated: 22/12/2023

Views: 6185

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.