1

我正在尝试编写一个 sql 查询,该查询取决于用户选择的内容,每 x 周每 x 天重复一次。因此,用户将选择他们希望作业每 2 周在星期二重复一次,提供的值是

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 日期。

提前致谢

4

3 回答 3

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 个超链接。这就是为什么 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)
于 2011-12-16T16:35:52.437 回答
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
于 2011-12-16T16:35:57.150 回答
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
于 2011-12-19T11:02:19.743 回答