SQL Server DATEDIFF() Function

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

DATEDIFF() offers a streamlined approach to measure intervals between dates, a fundamental capability in data analysis and reporting. By specifying a time unit and two dates, users can effortlessly quantify periods, enhancing the efficiency of temporal evaluations.

What is the DATEDIFF() function?

The DATEDIFF() function is a powerful date manipulation tool used to calculate the difference between two dates. It is designed to work across various platforms, providing flexibility in handling date-related data. The function is mainly known for its precision in measuring time spans by requiring three essential parameters: the unit of measurement for the interval (such as days, months, or years), and the two dates (start and end) for which the difference is to be calculated.

The function is widely supported across different database systems, making it a standard choice for date calculations. It is especially compatible with systems like Azure SQL and Analytics Platform System, starting from versions as early as SQL Server 2008.

DATEDIFF() is predominantly used within data manipulation functions. Its ability to break down time intervals into specific units makes it invaluable for tasks requiring detailed time-based analysis, reporting, or any scenario where understanding the duration between dates is crucial.

Syntax Of DATEDIFF()

Parameter of DATEDIFF()

The DATEDIFF() function requires three parameters to operate:

  1. interval: Defines the unit of time for the difference calculation between the two dates. It specifies the granularity of the result, such as years, months, days, etc.

The interval parameter accepts various values to cater to different levels of time granularity:

IntervalAbbreviationsDescription
yearyyyy, yy, yCalculates difference in full calendar years.
quarterqq, qDetermines the number of quarters between dates.
monthmm, mComputes the number of months between dates.
dayofyear-Finds the difference in days since the start of the year.
daydd, dCalculates the total days between dates.
weekww, wkMeasures the number of weeks between dates.
weekdaydw, wCounts the weekdays between dates.
hourhhComputes the hours difference between times.
minutemi, nCalculates the minutes difference.
secondss, sDetermines the seconds difference.
millisecondmsMeasures the milliseconds difference.
  1. start_date: The initial date from which the calculation begins. It acts as the baseline for the interval calculation.

  2. end_date: The final date up to which the difference is calculated. It marks the endpoint of the interval.

Return Type Of DATEDIFF()

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()

Example 1: Calculate the number of days it took for orders to be delivered, from the order date to the delivery date.

Dummy Data Set:

order_idorder_datedelivery_date
10012022-07-012022-07-04
10022022-07-022022-07-05
10032022-07-032022-07-06

Solution:

order_iddelivery_days
10013
10023
10033

Example 2: Determine the tenure of employees in years based on their start date up to the current date.

Dummy Data Set:

employee_idstart_date
E0012015-01-10
E0022017-03-15
E0032019-06-20

Solution: Assuming the current date is 2023-02-15,

employee_idyears_of_service
E0018
E0026
E0034

Example 3: Analyze the month-over-month sales growth by comparing the sales figures from the current month to the previous month.

Dummy Data Set:

sale_datesales_amount
2022-05-015000
2022-06-015500
2022-07-016000

Solution:

sale_yearsale_monthsales_amountprevious_month_salesgrowth
202255000NULLNULL
2022655005000500
2022760005500500

Example 4: Identify the ageing of unresolved support tickets by calculating the number of days since they were opened.

Dummy Data Set:

ticket_idopened_datestatus
T012023-01-01Open
T022023-01-15In-Progress
T032023-02-01Open

Solution: Assuming the current date is 2023-02-15,

ticket_iddays_open
T0145
T0231
T0314

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 will return an integer value that represents 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.