从今天到工作日到年底之间的工作日数

时间:2012-02-09 14:55:14

标签: sql

请帮忙。

尝试计算今天与工作人数之间的工作天数,直到今年年底为止。

我已经开始使用以下..日期差异仅执行今天和年底之间的天数..但我需要工作日 - 即cal.Weekday =''或HOLIDAY.DateInfo> “ '

CASe
  when  cal.Weekday = ' 'OR HOLIDAY.DateInfo>' ' then 0 else 1 end as workingdays,
  DateDiff("y",GETDATE(),DATEADD(s,-1,DATEADD(YY, DATEDIFF(YYYY,0,@StartDate)+1,0)) ) as enddateddd,

1 个答案:

答案 0 :(得分:1)

这会给你工作日(仅限M-F),因为假期我不知道你的假期是什么,所以你需要添加那个部分:

DECLARE @today AS DATE = GETDATE();
DECLARE @firstOfNextYear AS DATE = CAST('1/1/' + STR(1 + YEAR(GETDATE())) AS DATE);

WITH L0 AS (SELECT 1 AS C UNION ALL SELECT 1),       --      2
     L1 AS (SELECT L0.C FROM L0 CROSS JOIN L0 AS B), --      4
     L2 AS (SELECT L1.C FROM L1 CROSS JOIN L1 AS B), --     16
     L3 AS (SELECT L2.C FROM L2 CROSS JOIN L2 AS B), --    256
     L4 AS (SELECT L3.C FROM L3 CROSS JOIN L3 AS B), --  65536
      N AS (SELECT ROW_NUMBER() OVER(ORDER BY L4.C) AS n FROM L4),
      D AS (SELECT DATEADD(DAY, n - 1, @today) AS d, n FROM N)
SELECT COUNT(*) FROM D
WHERE d < @firstOfNextYear
  AND DATEPART(WEEKDAY, d) BETWEEN 2 AND 6