Labor Day Vs. Thanksgiving

跟風遠走 提交于 2019-12-04 07:50:30

I am stumped on figuring out Thanksgiving day (Thursday of the last FULL week of November).

Last Saturday of November - 2

Take the last Saturday of November, and subtract two days ;)

WITH    cal AS
        (
        SELECT  CAST('2009-30-11' AS DATETIME) AS cdate
        UNION ALL
        SELECT  DATEADD(day, -1, cdate)
        FROM    cal
        WHERE   cdate >= '2009-01-11'
        )
SELECT  TOP 1 DATEADD(day, -2, cdate)
FROM    cal
WHERE   DATEPART(weekday, cdate) = 6

For the complex algorithms, it's sometimes better to find a matching date from a set than trying to construct an enormous single-value formula.

See this article in my blog for more detail:

declare @thedate datetime
set @thedate = '11/24/2011'

select 1 
where datepart(dw, @thedate) = 5 and datepart(m, @thedate) = 11 AND (datepart(dd, @thedate) - 21) between 0 and 6

Is the date a Thursday in November and is there less than a week remaining.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!