List of distinct years between two dates
Tested on SQL Server 2008
declare @smaller_date date = convert(date, '25/12/2006', 103)
declare @larger_date date = convert(date, '14/11/2013', 103)
declare @diff int
select @diff = DATEDIFF(YY, @smaller_date, @larger_date)
;with sequencer(runner) as(
select 0
union all
select sequencer.runner + 1 from sequencer
where runner < @diff
)
select YEAR(@smaller_date) + runner from sequencer
A date like 1/2/2013
is ambiguous: depending on the regional setting, it can be either Feb 1st, or Jan 2nd. So it's a good idea to use the YYYY-MM-DD
date format when talking to the datebase.
You can generate a list of numbers using a recursive CTE:
; with CTE as
(
select datepart(year, '2006-12-25') as yr
union all
select yr + 1
from CTE
where yr < datepart(year, '2013-11-14')
)
select yr
from CTE
Example at SQL Fiddle.