2011-12-14 100 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