sql server - Sql Get months Name between two dates in a table -
my table has columns id , startdate , enddate
i need use way every row of table :
not specific value :
declare @start date = '2011-05-30' declare @end date = '2011-06-10' ;with months (date) ( select @start union select dateadd(month,1,date) months dateadd(month,1,date)<= dateadd(s,-1,dateadd(mm, datediff(m,0,@end)+1,0)) ) select datename(month,date) months
is possible ??
if properly, might looks as:
declare @tbl table (id int, start date, finish date) insert @tbl values (1, '2011-05-30', '2011-06-10'), (2, '2011-08-14', '2011-08-29'), (3, '2012-01-01', '2012-09-15') ;with periods as( select id, start, finish @tbl ) ,months(id, date) ( select id, start @tbl union select tbl.id, dateadd(month,1,months.date) months inner join @tbl tbl on tbl.id = months.id dateadd(month,1,months.date)<= dateadd(s,-1,dateadd(mm, datediff(m,0,finish)+1,0)) ) select id, datename(month,date) months order id
if need concatenate months' names, can replace last select smth this:
select id, ( select datename(month,date) + ' ' 'text()' months i.id = o.id xml path('') ) months o group o.id
Comments
Post a Comment