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

SQL datediff() Function

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. The segment of the date to compare comes from the intervals mentioned in the DATEADD function.
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?
You will quickly realize that the value returned is a rounded value, representing the nearest whole, integer value. So, for the number of months between the two dates, the result is 1.

Microsoft SQL Server


datediff() returns the number of specified time intervals between two dates

datediff() function in SQL

Use datediff() to count the days between two dates

Use datediff() to count the months between two dates