说实话,我有以下定义:
DECLARE @referenceDate DATETIME
SET @referenceDate = '20171123';
DECLARE @intervalDays INT
SET @intervalDays = 30
DECLARE @weekMonday DATETIME
SET @weekMonday = '20171204';
DECLARE @weekSunday DATETIME
SET @weekSunday = '20171210';
恰好在这些日期发生了重要的事情:
@referenceDate
DATEADD(day, 1 * @intervalDays, @referenceDate);
DATEADD(day, 2 * @intervalDays, @referenceDate);
DATEADD(day, 3 * @intervalDays, @referenceDate);
等等。
我想知道这件事是否重要'将(也)发生在@weekMonday和@weekSunday之间的指定周内。
我已获得SQL Server版本11.0.6251.0标准版,德语本地化。谢谢你的帮助。
答案 0 :(得分:1)
我用模数找到了这个解决方案:
IF (DATEDIFF(day, @referenceDate, @weekMonday) % @intervalDays = 0 OR
DATEDIFF(day, @referenceDate, DATEADD(day, 1, @weekMonday)) % @intervalDays = 0 OR
DATEDIFF(day, @referenceDate, DATEADD(day, 2, @weekMonday)) % @intervalDays = 0 OR
DATEDIFF(day, @referenceDate, DATEADD(day, 3, @weekMonday)) % @intervalDays = 0 OR
DATEDIFF(day, @referenceDate, DATEADD(day, 4, @weekMonday)) % @intervalDays = 0 OR
DATEDIFF(day, @referenceDate, DATEADD(day, 5, @weekMonday)) % @intervalDays = 0 OR
DATEDIFF(day, @referenceDate, DATEADD(day, 6, @weekMonday)) % @intervalDays = 0)
SELECT @monday AS found
也许有人知道如何进一步简化它?感谢。
答案 1 :(得分:0)
我不清楚你在找什么。
如果在范围内:
DECLARE @referenceDate DATETIME
SET @referenceDate = '20171123';
DECLARE @intervalDays INT
SET @intervalDays = 30
DECLARE @weekMonday DATETIME
SET @weekMonday = '20171204';
DECLARE @weekSunday DATETIME
SET @weekSunday = '20171210';
SELECT
CASE
WHEN DATEADD(day, @intervalDays, @referenceDate) BETWEEN @weekMonday AND @weekSunday
THEN @intervalDays
WHEN DATEADD(day, @intervalDays + 30, @referenceDate) BETWEEN @weekMonday AND @weekSunday
THEN @intervalDays + 30
ELSE 0
END AS [IsReferenceDatePlusIntervalDaysWithinTheDateRange]
到目前为止的天数:
SELECT DATEDIFF(day, @referenceDate, @weekMonday) AS [NumberOfDaysBetweenReferenceDateToWeekMonday]