SQL Functions   «Prev  Next»
Lesson 14 The DATEDIFF function
Objective Learn how to use the DATEDIFF function.

SQL datediff() Function

The DATEDIFF() function in SQL is used to calculate the difference between two dates. It returns the difference in the form of a specified time unit. The syntax of the DATEDIFF() function is as follows:
DATEDIFF(unit, date1, date2)

Here, unit is the type of unit you want the function to return. This could be YEAR, QUARTER, MONTH, DAY, HOUR, MINUTE, SECOND, etc., depending on the specific SQL dialect you are using. date1 and date2 are the two dates you want to compare. Let's say you have a employees table, and you want to find out the number of days between the hire_date and termination_date for each employee. Your SQL query would look like this:
SELECT
employee_id,
DATEDIFF(DAY, hire_date, termination_date) AS employment_duration
FROM
employees;

This query will return a list of employee IDs along with the duration of their employment in days. Do note that the order of the dates in the DATEDIFF() function matters. DATEDIFF(DAY, date1, date2) will return a positive number if date2 is after date1, and a negative number if date2 is before date1. Lastly, the exact syntax and behavior of the DATEDIFF() function can vary between different SQL dialects, so always check your platform's SQL documentation for precise usage instructions.


If you want to determine what the difference is between two dates, you can use the DATEDIFF() function. To use this function, indicate what segment of the date to compare, then provide the two dates.
DATEDIFF datepart options include:
  • year, quarter, month, dayofyear, day, week, weekday, hour, minute, second, millisecond, microsecond, nanosecond

DATEADD interval options include:
  • year, quarter, month, dayofyear, day, week, hour, minute, second, millisecond, microsecond, nanosecond

Key Differences:
  • `DATEDIFF` has `weekday` which `DATEADD` doesn't.
  • `DATEADD` has `quarter` which `DATEDIFF` doesn't have directly (you'd need to calculate it based on months).

The syntax is:
SELECT DateDiff(interval, first date, second date)

So, to determine the difference in weeks between 5/2/1998 and 6/15/1998, you can use the following statement:
SELECT DateDiff(week, '5/2/1998', '6/15/1998')

The difference is 7 weeks.
Now try what is the difference in
  1. days?
  2. Months?
  3. Years?
The function always returns an integer difference without rounding. So, for the number of months between the two dates, the result is 1.

SQL Guide

Microsoft SQL Server datediff()

datediff() returns the number of specified time intervals between two dates
The `datediff()` function in SQL Server is used to calculate the difference between two dates. It returns an integer that represents the difference between the start date and end date, based on the specified date part (e.g., day, month, year).
Syntax
DATEDIFF(datepart, startdate, enddate)
  1. `datepart`: This is the part of the date to calculate the difference on. It can be year, quarter, month, day, hour, minute, second, etc.
  2. `startdate`: The starting date or time from which to calculate the difference.
  3. `enddate`: The ending date or time to which the difference is calculated.

Common `datepart` values:
  1. `year` or `yy` or `yyyy`: Difference in years.
  2. `quarter` or `qq` or `q`: Difference in quarters.
  3. `month` or `mm` or `m`: Difference in months.
  4. `dayofyear` or `dy` or `y`: Difference in day of the year.
  5. `day` or `dd` or `d`: Difference in days.
  6. `hour` or `hh`: Difference in hours.
  7. `minute` or `mi` or `n`: Difference in minutes.
  8. `second` or `ss` or `s`: Difference in seconds.

Example Usage
-- Calculate the difference in days between two dates
SELECT DATEDIFF(day, '2024-01-01', '2024-08-21') AS DifferenceInDays;

Output:
`233` (This would be the number of days between January 1, 2024, and August 21, 2024.)
Key Points:
  • The order of dates matters: The `DATEDIFF()` function subtracts the start date from the end date. So if the start date is later than the end date, the result will be negative.
  • Result is always an integer: The function does not return fractional values, so if you are calculating differences in a unit smaller than the given datepart (e.g., minutes in a day difference), it still returns an integer.

Another Example:
-- Calculate the difference in months between two dates
SELECT DATEDIFF(month, '2023-01-15', '2024-08-15') AS DifferenceInMonths;

Output:
`19` (This would be the number of months between January 15, 2023, and August 15, 2024.)
In summary, `DATEDIFF()` is a versatile function that allows you to measure the time difference between two dates using various units, depending on the `datepart` you specify.
datediff() returns the number of specified time intervals between two dates
Use datediff() to count the days between two dates

datediff() returns the number of specified time intervals between two dates
Use datediff() to count the months between two dates

SEMrush Software