-- sql server 获取上周几和本周几 -- @@DATEFIRST 每周以周几开始,周一是 1 ,周日是 7 ,有些机器默认是7,有些机器默认是1 SELECT @@DATEFIRST -- 修改 @@DATEFIRST 的值 SET DATEFIRST 7 -- 查询当前每周是以周几开始,今天是一周的第几天 SELECT @@DATEFIRST as 'startday', DATEPART(dw,GETDATE()) as 'weekday' -- 通过 WEEKDAY 和 @@DATEFIRST 联合计算,抹平 @@DATEFIRST 设置的差异 -- 上周 SELECT CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) - 6, GETDATE()) ) as '上周一' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) - 5, GETDATE()) ) as '上周二' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) - 4, GETDATE()) ) as '上周三' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) - 3, GETDATE()) ) as '上周四' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) - 2, GETDATE()) ) as '上周五' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) - 1, GETDATE()) ) as '上周六' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) - 0, GETDATE()) ) as '上周日' -- 本周 SELECT CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) + 1, GETDATE()) ) as '本周一' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) + 2, GETDATE()) ) as '本周二' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) + 3, GETDATE()) ) as '本周三' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) + 4, GETDATE()) ) as '本周四' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) + 5, GETDATE()) ) as '本周五' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) + 6, GETDATE()) ) as '本周六' ,CONVERT(DATE, DATEADD(dd, -DATEPART (WEEKDAY ,GETDATE() + @@DATEFIRST - 1) + 7, GETDATE()) ) as '本周日'