sql-Weekly date range per week

Source: Internet
Author: User

--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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.