Problem: Time Span between two dates. I would like to know how many months are between each date. The trick is: the number of months in each year between the two dates.
For example: Start date = 1/1/2014 End Date = 3/1/2016
The output:
Column 1: "2014" would have a value of 12 Column 2: "2015" would have a value of 12 Column 3: "2016" would have a value of 2
This would be for a list with many dates (with different years)
EDIT: You would indeed have to have 14 year columns for a date span between 2000-2014. However, it is unlikely that more than 5 columns would need to be added.
Current train of thought
declare @datediff as int
select
@datediff=(Datediff(MONTH,[begin date], [end date]))
from [DateRange]
select
case
when @datediff <= 12 then @datediff
when @datediff <= 24 then @datediff -12
when @datediff <= 36 then @datediff -24
when @datediff <= 48 then @datediff -36
else NULL
end
from [DateRange]
Any ideas on this one?
I am very new to SQL and was only able to get the total months between the two with the following code:
select
datediff(MONTH,[begin date], [end date])
from [tableofdates]