Finding the Difference Between Two Dates : DATEDIFF « Date Functions « SQL Server / T-SQL Tutorial






DATEDIFF subtracts the first date from the second date.
The syntax for DATEDIFF is:

DATEDIFF ( datepart , startdate , enddate )

The first datepart code uses the same datepart codes as DATEADD.
The second and third arguments are the date values that are part of the subtraction.








10.4.DATEDIFF
10.4.1.Finding the Difference Between Two Dates
10.4.2.DATEDIFF returns the difference between two dates as specified by datepart: DATEDIFF(datepart,date1,date2)
10.4.3.select DATEDIFF(day, '2001-12-01', '2002-09-30')
10.4.4.select DATEDIFF(month, '2001-12-01', '2002-09-30')
10.4.5.select DATEDIFF(year, '2001-12-01', '2002-09-30')
10.4.6.select DATEDIFF(hour, '06:46:45', '11:35:00')
10.4.7.select DATEDIFF(minute, '06:46:45', '11:35:00')
10.4.8.select DATEDIFF(second, '06:46:45', '11:35:00')
10.4.9.select DATEDIFF(quarter, '2001-12-01', '2002-09-30')
10.4.10.select DATEDIFF(week, '2001-12-01', '2002-09-30')
10.4.11.select DATEDIFF(day, '2002-09-30', '2001-12-01')
10.4.12.SELECT DATEDIFF(yy, 'Jan 1, 1998', 'Dec 31, 1998')
10.4.13.SELECT DATEDIFF(yy, 'Dec 31, 1998', 'Jan 1, 1999')
10.4.14.Find difference in months between now and EndDate
10.4.15.A SELECT statement that computes the age of a Billing with DATEDIFF
10.4.16.Days between two dates