Thursday, March 27, 2014

Unpivot table between two dates

Kadir,


Check this:



declare @tab table (id int identity(1,1),name varchar(100),subject varchar(100),start_Date datetime,end_date datetime)

insert @tab select 'Kadir Ozzgur','(holiday)','2014-07-12','2014-07-24'


select * from @tab

;with cte
as
(
select id,name,subject,start_date as mydate,end_date
from @tab
UNION ALL
select t.id,t.name,c.subject,dateadd(dd,1,c.mydate) as mydate,t.end_date
from cte c
join @tab t on c.id=t.id
where dateadd(dd,1,c.mydate)<=t.end_date
)

select *
from cte
order by id





Thanks,

Jay

<If the post was helpful mark as 'Helpful' and if the post answered your query, mark as 'Answered'>


No comments:

Post a Comment