DECLARE @mindate Date, @Maxdate Date SELECT @mindate = MIN(StartDate), @Maxdate = MAX(EndDate) FROM SourceData ; WITH Dates AS ( SELECT Date = @mindate UNION ALL SELECT Date = DATEADD(DAY, 1, Date) FROM Dates WHERE Date < @maxDate ) SELECT Name, Date FROM Sourcedata INNER JOIN DATES ON date between startdate AND enddate OPTION (MAXRECURSION 32635)The bare bone approach from my previous post seems faster and I prefer that one as I really hate CTE's despite their obvious strenght.
>create table dbo.SourceData (Name varchar(100), StartDate date, EndDate date) > >insert into dbo.SourceData values > ('Kevin Goff', '9-12-2016', '9-12-2016'), > ('Katy Goff','9-12-2016', '9-15-2016') >>
>CREATE FUNCTION dbo.ExplodeDates >( @StartDate date, @EndDate date) >returns table >as >return ( > with tempcte as > (select @StartDate as ActualDate > union all -- CTE recursive loop > select dateadd(d,1,ActualDate) from tempcte > where dateadd(d,1,ActualDate) <= @EndDate ) > select * from tempcte ) > >go > > >select Name, ActualDate from SourceData > CROSS APPLY dbo.ExplodeDates ( SourceData.StartDate, SourceData.EndDate) > OPTION (MAXRECURSION 32767) >>
>Name ActualDate
>Kevin Goff 2016-09-12
>Katy Goff 2016-09-12
>Katy Goff 2016-09-13
>Katy Goff 2016-09-14
>Katy Goff 2016-09-15
>
>