2011-12-14 118 views
1

我試圖寫一個sql查詢,這取決於用戶選擇什麼會每隔x周每隔x天發生一次。 因此用戶選擇他們想要的工作再次發生在星期二每2周 所提供的值是SQL每x周工作日每x周

declare @StartDate datetime -- when the job first recurs 
declare @recurrenceValue1 int -- amount of weeks 
declare @recurrenceValue2 int -- day of week (mon-sun) 
declare @NextOcurrance datetime -- when the job will recur 

我知道如何設置好幾個星期的量:

SET @NextOccurance = (Convert(char(12),@StartDate + (@RecurrenceValue1),106)) 

但我不確定如何讓它滾到星期幾,所以如果@startDate是今天,它應該在星期二每2周重複一次,它會看到今天的2周是星期三,所以會循環直到它知道這一天是星期二,那將是@NextRecurrance日期。

在此先感謝

+0

這是SQLServer的? – 2011-12-14 12:27:26

+0

嗨呀是的 - 我使用sqlserver2008 – anna 2011-12-14 12:37:45

回答

2

一個簡單的方法,以週數增加的日期是使用(MSDN DATEADD

DATEADD(wk, @StartDate, @recurrenceValue1) 

要找出一週中的一天的日期,你正在尋找屬於,你可以使用(MSDN DATEPART

DATEPART(dw, @StartDate) 

此功能使用DATEFIRST,以確定哪些星期幾是第一個(http://msdn.microsoft。 COM/EN-US /庫/ ms181598.aspx)

SET DATEFIRST 1 --Where 1 = Monday and 7 = Sunday 

因此,對於你的問題(DATEFIRST被設置爲1 =星期一)..

SET DATEFIRST 1 

declare @StartDate datetime -- when the job first recurs 
declare @recurrenceValue1 int -- amount of weeks 
declare @recurrenceValue2 int -- day of week (mon-sun) 
declare @NextOcurrance datetime -- when the job will recur 


SET @StartDate = '2011-12-16' -- This is a Friday 
SET @recurrenceValue1 = 2 -- In 2 weeks 
SET @recurrenceValue2 = 2 -- On Tuesday 
SET @NextOcurrance = DATEADD(wk, @recurrenceValue1, @StartDate) -- Add our 2 weeks 
/* Check if our incrementation falls on the correct day - Adjust if needed */ 
IF (DATEPART(dw, @NextOcurrance) != @recurrenceValue2) BEGIN 
    DECLARE @weekDay int = DATEPART(dw, @NextOcurrance) 
    SET @NextOcurrance = DATEADD(dd, ((7 - @weekDay) + @recurrenceValue2), @NextOcurrance) -- Add to @NextOcurrance the number of days missing to be on the requested day of week 
END 

天增加的數量的邏輯如下: 我們一週有7天,需要多少天才能達到本週末。將此天數添加到@ recurrenceValue2(我們正在查找的星期幾)。注:因爲我的聲望,我不能發佈超過2個超鏈接。這就是爲什麼DATEFIRST URL是純文本的。


下面是一些代碼,可以對某些特定的日期進行不同的處理。雖然此代碼僅適用於獨特的日期。例如,如果需要跳過整整一週,則使用此代碼將需要爲本週的每一天添加值以跳過。對於除獨特日子以外的範圍,應修改此代碼以處理日期範圍或特定星期和/或星期幾。

CREATE TABLE OccurenceExclusions (
    ExclusionDate DATE not null, 
    NumberOfDaysToAdd int not null 

    PRIMARY KEY (ExclusionDate) 
) 

INSERT OccurenceExclusions VALUES ('2012-01-01', 7) 

SET @NextOcurrance = DATEADD(dd, COALESCE((SELECT NumberOfDaysToAdd 
          FROM OccurrenceExclusions 
          WHERE ExclusionDate = @NextOcurrance), 0), @NextOcurrance) 
0

使用@DanielM列明的原則,我改變它適合我的查詢見下圖:

BEGIN 
    SET DATEFIRST 1 --Where 1 = Monday and 7 = Sunday 
    declare @StartDate datetime -- when the job first recurs 
    declare @recurrenceValue1 int -- amount of weeks 
    declare @recurrenceValue2 int -- day of week (mon-sun) 
    declare @NextOcurrance datetime -- when the job will recur 
         -- sets @nextoccurence to next date after x amount of weeks 
SET @NextOccurance = DATEADD(wk, @recurrenceValue1, @StartDate) -- Add on weeks /* Check if our incrementation falls on the correct day - Adjust if needed */ 
         IF (DATEPART(dw, @NextOccurance) != @recurrenceValue2) 
         BEGIN 
         DECLARE @Weekday int = DATEPART(dw, @NextOccurance)  
         SET @NextOccurance = DATEADD(dd, (@RecurrenceValue2 - @Weekday), @NextOccurance) -- Add to @NextOcurrance the number of days missing to be on the requested day of week 
         END 
        END