>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
>
>CREATE FUNCTION [dbo].ft_Rows ( @max INT ) RETURNS TABLE RETURN SELECT TOP (@max) Nulls = NULL FROM master.sys.all_columns T1 -- ~ 7000 -- put here the long master system table , master.sys.all_columns T2 -- ~ 7000² more 55 millions -- , master.sys.all_columns T3 -- ~ 7000³ more 400 billions -- add this get 2 x slower GO SELECT * , ActualDate = DATEADD(D,P.N-1,S.StartDate) FROM dbo.SourceData S CROSS APPLY ( SELECT N=ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM dbo.ft_Rows(1+DATEDIFF(D,S.StartDate,S.EndDate))) P