Get date difference
To get date difference, difference between two dates you can use DATEDIFF function.
DATEDIFF function returns the difference of the specified datepart between the two dates(start date and end date).
DATEDIFF
DATEDIFF ( datepart , startdate , enddate )
Year
Get years from two dates.
DECLARE @start_date datetime = '2020-01-01 22:24:10';
DECLARE @end_date datetime = '2022-01-01 22:24:10';
SELECT DATEDIFF(year, @start_date, @end_date) as Result;
Result: 2
Month
Get months from two dates.
DECLARE @start_date datetime = '2021-09-01 22:24:10';
DECLARE @end_date datetime = '2022-01-01 22:24:10';
SELECT DATEDIFF(month, @start_date, @end_date) as Result;
Result: 4
Day
Get days from two dates.
DECLARE @start_date datetime = '2021-09-01 22:24:10';
DECLARE @end_date datetime = '2022-01-01 22:24:10';
SELECT DATEDIFF(day, @start_date, @end_date) as Result;
Result: 122
Week
Get weeks from two dates.
DECLARE @start_date datetime = '2021-09-01 22:24:10';
DECLARE @end_date datetime = '2022-01-01 22:24:10';
SELECT DATEDIFF(week, @start_date, @end_date) as Result;
Result: 17
Hour
Get hours from two dates.
SELECT DATEDIFF(hour, GETDATE(), GETDATE()+2);
Result: 48