weekday = 'Friday' (or whichever way to determine if this day is Friday)
and day_of_month between 12 and 18
如果星期一开始,这将有效 .
0
如果工作周在星期一开始,那么一个月的第二个完整工作周的星期五是
SELECT DATEADD(day, (9 - DATEPART(weekday, @fdom)) % 7) + 11, @fdom)
AS SecondWorkweekFriday;
-- 1. start with the first day of the month (@fdom)
-- 2. advance to the nearest Monday with modular arithmetic
-- n.b.: by default DATEPART considers Monday to be ordinal
-- week day #2, thus "7 - DATEPART() + 2" becomes
-- "9 - DATEPART"
-- 3. add eleven days to get to the second Friday thereafter
3 回答
这可能一开始并不容易理解,但对于这个特定情况,规则很简单:
如果星期一开始,这将有效 .
如果工作周在星期一开始,那么一个月的第二个完整工作周的星期五是
@fdom是这个月的第一天 . 有很多方法可以在SQL中找到first day of the month .
我个人喜欢这样的日期表,但你也可以算出来:
可能需要调整
5
,但我认为这是正确的,因为datepart(weekday
是基于星期日的,你是星期一(可能需要6才能正确抵消) .无论如何,这应该让你进入球场 .