Попробуйте этот код:
--declaration of variables
declare @frommonth char(3) = 'jan',@fromyear char(4) = 2011,
@tomonth char(3) = 'APR', @toyear char(4) = 2011
declare @output varchar(max)
declare @f int, @t int
select --setting from and to month as months after 1900-01-01
@f = datediff(month, 0, cast('1' +@frommonth+@fromyear as datetime)),
@t = datediff(month, 0, cast('1' +@tomonth+@toyear as datetime))
-- recusive loop
;with cte as
(
select @f m
union all
select m + 1 from cte
where m < @t
)
select @output = coalesce(@output +',', '') +stuff(convert(varchar(11),dateadd(mm, m, 0), 109), 4, 6, '''') FROM CTE
select @output
Результат:
Jan'11,Feb'11,Mar'11,Apr'11
Тест здесь:
http://data.stackexchange.com/stackoverflow/q/114801/declaration-of-variables