declare @from datetime = '2025-08-16', @to datetime='2025-11-15'
select DATEDIFF(month, @from, @to) - case when DATEPART(day, @from) > DATEPART(day, @to) then 1 else 0 end as months
This calculates months elapsed between two dates from date to date. Subtracts 1 if the day part of @from is greater than that of @to.
These are the results:
@from = '2025-01-27', @to='2025-02-28' => 1
@from = '2025-01-28', @to='2025-02-28' => 1
@from = '2025-01-29', @to='2025-02-28' => 0
@from = '2025-01-30', @to='2025-02-28' => 0
@from = '2025-01-31', @to='2025-02-28' => 0
@from = '2025-01-31', @to='2025-03-01' => 1
@from = '2025-08-16', @to='2025-09-15' => 0
@from = '2025-08-16', @to='2025-09-16' => 1
@from = '2025-08-16', @to='2025-11-15' => 2
@from = '2025-08-16', @to='2025-11-16' => 3
@from = '2025-08-16', @to='2025-11-30' => 4