if object_id('[dbo].[fn_GetDateRange]') is not null
drop function [dbo].[fn_GetDateRange]
go
create function [dbo].[fn_GetDateRange](@BaseDate datetime, @DaysBefore int, @DaysAfter int)
returns @Dates Table
(theDate datetime
)
as
/************************************************************
*
* Author: Brandon Galderisi
* Last modified: 07-Oct-2008
* Purpose: Given a base date, returns a range of
* dates from @DaysBefore before the @BaseDate
* to @DaysAfter the @BaseDate.
*
*
*************************************************************/
begin
declare @minDate datetime
set @minDate=dateadd(d,-@DaysBefore,@BaseDate)
;with
cte0 as (select 1 as c union all select 1),
cte1 as (select 1 as c from cte0 a, cte0 b),
cte2 as (select 1 as c from cte1 a, cte1 b),
cte3 as (select 1 as c from cte2 a, cte2 b),
cte4 as (select 1 as c from cte3 a, cte3 b),
cte5 as (select 1 as c from cte4 a, cte4 b),
nums as (select row_number() over (order by c) as n from cte5)
insert into @Dates
select @MinDate
union
select dateadd(d,n,@MinDate) from nums
where n<=@DaysBefore+@DaysAfter
order by 1
return
end
go
|