2013-09-27 83 views
2

如何編寫一個查詢以返回兩個日期之間的工作日星期的開始和結束日期?SQL Server查詢返回工作日星期開始,結束日期

例子:

declare 
    @startDate date = '2013-07-01' 
    , @endDate date = '2013-09-30' 

SELECT WeekData(@startDate, @endDate) 

結果:

Name, Week1Start, Week1End, Week2Start, Week2End, Week3Start, Week3End, Week4Start, Week4End, Week5Start, Week5End 
--------------------------------------------------------------------------------------------------------------------------------------------------- 
'July 2013', '2013-07-01', '2013-07-05', '2013-07-08', '2013-07-12', '2013-07-15', '2013-07-19', '2013-07-22', '2013-07-26', '2013-07-29', '2013-07-31' 
'August 2013', '2013-08-01', '2013-08-02', '2013-08-05', '2013-08-09', '2013-08-12', '2013-08-16', '2013-08-19', '2013-08-23', '2013-08-26', '2013-08-30' 
'September 2013', '2013-09-02', '2013-09-06', '2013-09-09', '2013-09-13', '2013-09-16', '2013-09-20', '2013-09-23', '2013-08-27', '2013-09-30', '2013-09-30' 

一個工作周從星期一開始或每月的第一天,並在週五或每月的最後一天結束。

+1

可能重複[什麼是獲得的最好方式SQL中兩個日期之間的開始日期和結束日期](http://stackoverflow.com/questions/12752136/what-is-the-best-way-to-get-start-date-and-enddate -of-的周間兩日) –

回答

2

輸出是否必須採用確切的給定格式?如果你想要一個完整的匹配日期列表,我想到的方法是使用某種T-SQL循環,引用DATEPART(或DATENAME,如前面的答案)。

DECLARE @startDate DATETIME = '7/1/2013', @endDate DATETIME = '9/30/2013'; 

DECLARE @currentDate DATETIME = @startDate; 
DECLARE @currentMonth INT, @currentDay INT, @currentDayOfWeek INT; 
DECLARE @prevMonth INT = 0, @prevDay INT = 0; 

WHILE @currentDate <= @endDate 
BEGIN 
    SET @currentMonth = MONTH(@currentDate); 
    SET @currentDay = DAY(@currentDate); 

    -- see if we need a month header 
    IF(@currentMonth <> @prevMonth) 
    BEGIN 
     PRINT 'Month: ' + CAST(@currentMonth AS VARCHAR) + '/' + CAST(YEAR(@currentDate) AS VARCHAR); 
     SET @prevMonth = @currentMonth; 
    END 

    -- see if it is a week start or end 
    SET @currentDayOfWeek = DATEPART(WEEKDAY, @currentDate); 
    IF((@currentDay = 1) 
     OR (@currentDay = 31) -- this would obviously need to be more sophisticated 
     OR (@currentDayOfWeek = 2) -- Monday 
     OR (@currentDayOfWeek = 6) -- Friday 
    ) PRINT @currentDate; 

    -- go on to the next date 
    SET @currentDate = @currentDate + 1; 
END 
0

猜猜我對派對有點遲,但我剛剛完成與這一個摔跤。這是我用來產生近兩年的工作周(假設太陽是一週,週六開始爲年底):

Declare @workWeeks Table (
    weekStart datetime, 
    weekEnd datetime, 
    weekText varchar(25) 
) 

Declare @count int, 
     @lastSunday datetime, 
     @nextSaturday datetime 
Select @count = 0, 
     @lastSunday = DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE())/7 * 7, -1), 
     @nextSaturday = DATEADD(MS, -3, DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE())/7 * 7, 6)) -- Next Sunday minus 3 ms 

While @count < 52 
Begin 
    Insert Into @workWeeks Values(
     DATEADD(WK, [email protected], @lastSunday), 
     DATEADD(WK, [email protected], @nextSaturday), 
     CONVERT(varchar, DATEADD(WK, [email protected], @lastSunday), 101) + ' - ' + CONVERT(varchar, DATEADD(WK, [email protected], @nextSaturday), 101)) 
    Set @count = @count + 1 
End 

Select * From @workWeeks 

而且你會得到這樣的:

weekStart    weekEnd     weekText 
----------------------- ----------------------- ------------------------- 
2015-06-14 00:00:00.000 2015-06-20 23:59:59.997 06/14/2015 - 06/20/2015 
2015-06-07 00:00:00.000 2015-06-13 23:59:59.997 06/07/2015 - 06/13/2015 
2015-05-31 00:00:00.000 2015-06-06 23:59:59.997 05/31/2015 - 06/06/2015 
2015-05-24 00:00:00.000 2015-05-30 23:59:59.997 05/24/2015 - 05/30/2015 
... 

您可以通過在本例中分別設置@lastSunday和@nextSaturday,-1和6的表達式末尾的日期調整值來改變一週中的開始和結束日期。

但是,如果我正確地讀你的原始問題,你只是希望星期一到星期五對嗎?在這種情況下,僅僅通過一天的每個方向調整在INSERT語句中,像這樣:

-- For Mon thru Fri: 
Insert Into @workWeeks Values(
    DATEADD(WK, [email protected], DATEADD(DD, 1, @lastSunday)), 
    DATEADD(WK, [email protected], DATEADD(DD, -1, @nextSaturday)), 
    CONVERT(varchar, DATEADD(WK, [email protected], DATEADD(DD, 1, @lastSunday)), 101) + ' - ' + CONVERT(varchar, DATEADD(WK, [email protected], DATEADD(DD, -1, @nextSaturday)), 101)) 

而且你會得到這樣的:

weekStart    weekEnd     weekText 
----------------------- ----------------------- ------------------------- 
2015-06-15 00:00:00.000 2015-06-19 23:59:59.997 06/15/2015 - 06/19/2015 
2015-06-08 00:00:00.000 2015-06-12 23:59:59.997 06/08/2015 - 06/12/2015 
2015-06-01 00:00:00.000 2015-06-05 23:59:59.997 06/01/2015 - 06/05/2015 
2015-05-25 00:00:00.000 2015-05-29 23:59:59.997 05/25/2015 - 05/29/2015 
... 
相關問題