--date parameters, where you can establish a stored procedure, receive a month, calculate the start of a month, or directly accept the start and end timesDeclare @begDate datetime = '2014-06-01'Declare @endDate datetime = '2014-06-30'Declare @text varchar(Max)= "' while(@begDate <= @endDate)begin Select @text = @text + Case when Datename(DW,@begDate)= 'Monday' Then Case when Convert(varchar(Ten),DateAdd(DD,7,@begDate), -)<= @endDate Then Convert(varchar(Ten),@begDate, -)+ '~' + Convert(varchar(Ten),DateAdd(DD,6,@begDate), -)+ CHAR(Ten) Else Convert(varchar(Ten),@begDate, -)+ '~' + Convert(varchar(Ten),@endDate, -)+ CHAR(Ten) End when Datename(DW,@begDate)!= 'Monday' and DATEPART(DD,@begDate)= 1 Then Convert(varchar(Ten),@begDate, -)+ '~' + Convert(varchar(Ten),DateAdd(DD, Case when 8 - DATEPART(DW,@begDate)< 7 Then 8 - DATEPART(DW,@begDate) Else 0 End,@begDate), -)+ CHAR(Ten) Else "' End Select @begDate = DATEADD(DD,1,@begDate)EndPrint @text
sql-Weekly date range per week