0

I am trying to get the start and end dates of each week given a specific date range. For example, given the date range

BETWEEN '11-21-2016' AND '12-25-2016'

I need to get the following start and end dates:

'11-21-2016' AND '11-27-2016'
'11-28-2016' AND '12-04-2016'
'12-05-2016' AND '12-11-2016'
'12-12-2016' AND '12-18-2016'
'12-19-2016' AND '12-25-2016'

I found on here a few examples given a month name, example if given december it will do as explained above for that month. However, cannot find any examples that use a date range as the initial "month".

Azuraith
  • 1,030
  • 14
  • 28

2 Answers2

1

I often use a TVF to create dynamic date/time ranges. I tally table would do the trick as well. That said, the UDF is faster than a recursive approach for larger sets and offers a bit more functionality. For example, you define the date range, datepart, and increment

Select DateR1=RetVal,DateR2=DateAdd(DD,6,RetVal) from [dbo].[udf-Range-Date]('2016-11-21','2016-12-25','DD',7)

Returns

DateR1      DateR2
2016-11-21  2016-11-27
2016-11-28  2016-12-04
2016-12-05  2016-12-11
2016-12-12  2016-12-18
2016-12-19  2016-12-25

The UDF if desired

CREATE FUNCTION [dbo].[udf-Range-Date] (@R1 datetime,@R2 datetime,@Part varchar(10),@Incr int)
Returns Table
Return (
    with cte0(M)   As (Select 1+Case @Part When 'YY' then DateDiff(YY,@R1,@R2)/@Incr When 'QQ' then DateDiff(QQ,@R1,@R2)/@Incr When 'MM' then DateDiff(MM,@R1,@R2)/@Incr When 'WK' then DateDiff(WK,@R1,@R2)/@Incr When 'DD' then DateDiff(DD,@R1,@R2)/@Incr When 'HH' then DateDiff(HH,@R1,@R2)/@Incr When 'MI' then DateDiff(MI,@R1,@R2)/@Incr When 'SS' then DateDiff(SS,@R1,@R2)/@Incr End),
         cte1(N)   As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
         cte2(N)   As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a, cte1 b, cte1 c, cte1 d, cte1 e, cte1 f, cte1 g, cte1 h ),
         cte3(N,D) As (Select 0,@R1 Union All Select N,Case @Part When 'YY' then DateAdd(YY, N*@Incr, @R1) When 'QQ' then DateAdd(QQ, N*@Incr, @R1) When 'MM' then DateAdd(MM, N*@Incr, @R1) When 'WK' then DateAdd(WK, N*@Incr, @R1) When 'DD' then DateAdd(DD, N*@Incr, @R1) When 'HH' then DateAdd(HH, N*@Incr, @R1) When 'MI' then DateAdd(MI, N*@Incr, @R1) When 'SS' then DateAdd(SS, N*@Incr, @R1) End From cte2 )

    Select RetSeq = N+1
          ,RetVal = D 
     From  cte3,cte0 
     Where D<=@R2
)
/*
Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
Syntax:
Select * from [dbo].[udf-Range-Date]('2016-10-01','2020-10-01','YY',1) 
Select * from [dbo].[udf-Range-Date]('2016-01-01','2017-01-01','MM',1) 
*/
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
0

Use a numbers table, define start and end, find the dates between (My MSSQL is rusty, feel free to fix syntax)

declare @STARTDATE date;
declare @ENDDATE date;
set @STARTDATE = '2016-21-11';
set @ENDDATE = '2016-12-25';

with Nums as
(
select 1 as NN
union all
select NN+1 as NN
fom Nums
where NN < 1000
)
select dateadd(dd, NN, @STARTDATE) as DayStart, dateadd(dd, NN+6, @STARTDATE) as DayEnd
from Nums
where dateadd(dd, NN+6, @STARTDATE) <= @ENDDATE
and datepart(dw,dateadd(dd, NN, @STARTDATE))=1
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
JohnHC
  • 10,935
  • 1
  • 24
  • 40
  • 1
    You must place a `;` after the preceeding line. Often seen: `;WITH`, to avoid this... – Shnugo Nov 30 '16 at 15:38
  • @JohnHC you'll still need to terminate your previous statements (your `set` statements) before the cte. Have you tried to run this? `declare @startdate datetime set @startdate = getdate WITH CTE AS(SELECT 1 as a) SELECT * FROM CTE` – S3S Nov 30 '16 at 15:43
  • Thanks guys, totally forgot about that. – JohnHC Nov 30 '16 at 15:54