SQL每隔x个星期每x个工作日重复一次

时间:2011-12-14 11:56:05

标签: sql sql-server-2008 datetime recurrence

我正在尝试编写一个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周是星期三所以将循环直到它知道那天是星期二,这将是@NextRecurrance日期。

提前致谢

3 个答案:

答案 0 :(得分:2)

在日期中添加若干周的简便方法是使用(MSDN DATEADD

DATEADD(wk, @StartDate, @recurrenceValue1)

要查找您所查看日期的星期几,您可以使用(MSDN DATEPART

DATEPART(dw, @StartDate)

此函数使用DATEFIRST来确定一周中哪一天是第一天(http://msdn.microsoft.com/en-us/library/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(我们正在寻找的星期几)。

PS:由于我的声誉,我不能发布超过2个HyperLinks。这就是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)

答案 1 :(得分:0)

可能最好的解决方案是使用日历表。 http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

然后您可以这样查询:

SELECT TOP 1 @NextOccurance = Date
FROM CalendarTable
WHERE Date >= DATEADD(week, @recurranceValue1, @StartDate)
AND DateName = @recurranceValue2

答案 2 :(得分: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