DATEDIFF(): This
function will return difference between two dates in any of the following
datepart: Hours, days,Months &
years.
Syntax:
DATEDIFF(datepart, startdate,
enddate)
|
For Example:
Query to return
difference of hours between 2020-03-25 and 2015-03-26 dates in MS SQL Server.
Select DATEDIFF( HOUR, '2020-03-25' , '2020-03-26') as HourDifference
HourDifference: 24
|
Difference of days between 2020-05-25
and 2015-03-25
Select DATEDIFF(DAY, '2020-03-25' , '2020-07-25') as
DayDifference
DayDifference: 122
|
Difference of Months between
2020-05-25 and 2015-07-25
Select DATEDIFF( MONTH, '2020-03-25' , '2020-07-26') as
MonthDifference
MonthDifference: 4
|
Difference of years between 2020-05-25
and 2015-07-25
Select DATEDIFF( YEAR, '2013-03-25' , '2020-07-26') as
YearDifference
YearDifference: 7
|