SQL Server查询以获取两个日期(不包括节假日)之间的工作日数

SQL Server查询以获取两个日期(不包括节假日)之间的工作日数,sql,sql-server,date,sql-server-2012,Sql,Sql Server,Date,Sql Server 2012,我们正在使用SQL Server 在我们的WHEN语句中,我需要检查两个日期之间的天数是否大于3个工作日(因此不包括周末和节假日) 假设我有一个假日日历表,其中包含所有假日的HolidayDates列,例如:2018年12月25日、2018年12月31日等 度假日期 12/25/2018 12/31/2018 那么,如果 日期1=1/2/19(星期三) 日期2=18年12月27日(星期四) 日期1和日期2之间的工作日数为3天(12/27、12/28和12/31) 上述查询将获得包括周末和节假日

我们正在使用SQL Server

在我们的WHEN语句中,我需要检查两个日期之间的天数是否大于3个工作日(因此不包括周末和节假日)

假设我有一个假日日历表,其中包含所有假日的HolidayDates列,例如:2018年12月25日、2018年12月31日等

度假日期 12/25/2018 12/31/2018 那么,如果

日期1=1/2/19(星期三)

日期2=18年12月27日(星期四)

日期1和日期2之间的工作日数为3天(12/27、12/28和12/31)

上述查询将获得包括周末和节假日在内的工作日数

CASE WHEN end_date - start_date > 3  THEN 0  --> this need to exclude 
    weekend and holidays
WHEN CODE = 1 THEN 1
WHEN CODE =2 THEN 2
ELSE 3
END AS MyColumn
如何在查询中排除周末和假日

多谢各位

编辑答案:

select start_date, end_date,
datediff(day, mt.start_date, mt.end_date) datediff,
(select
 (datediff(wk, mt.start_date, mt.end_date) )
 +(case when datename(dw, mt.start_date) = 'sunday'   then 1 else 0 end)
 +(case when datename(dw, mt.end_date)   = 'saturday' then 1 else 0 end)
 ) weekend,
(select count(*) from HolidayDates hd
where hd.holydayDate between mt.start_date and mt.end_date
 ) as [holydays (not weekends)],
datediff(day, mt.start_date, mt.end_date)
-(select
(datediff(wk, mt.start_date, mt.end_date) )
+(case when datename(dw, mt.start_date) = 'sunday'   then 1 else 0 end)
+(case when datename(dw, mt.end_date)   = 'saturday' then 1 else 0 end)
) * 2
-(select count(*) from HolidayDates hd
 where hd.holydayDate between mt.start_date and mt.end_date
)
as diff
from MyTable mt
在下面的查询中,您可以看到如何计算列

[休息日(非周末)]:从您的表格中获取所有的休息日也不是周末(因此不计算两次)

周末:获取该时段的周末

列的其余部分可以是自解释的

免责声明,您可以将其简化一点,这只是如何使用的示例查询

返回

total days  holydays (not weekends) weekends    mycolumn
----------- ----------------------- ----------- -----------
7           2                       2           3
61          2                       18          0
9           2                       2           0

(3 row(s) affected)

Hi的可能重复,Oracle语法与SQL Server不同,例如SQL Server没有TRUNC(end_date,'IW')Hi,不计算两个日期之间的工作日数您知道在您的示例中,
Date1
大于
Date2
,并且不会返回任何内容。谢谢,斜纹棉布我使用了您的解决方案并对其进行了一些更新(我用最终解决方案编辑了我的问题)。
select 
  datediff(day, mt.start_date, mt.end_date) as [total days], 
  (
    select 
      count(*) 
    from 
      HolidayDates hd 
    where 
      hd.holydayDate between mt.start_date 
      and mt.end_date 
      and DATEPART(WEEKDAY, hd.holydayDate) between 2 
      and 6
  ) as [holydays (not weekends) ], 
  (
    select 
      (
        datediff(wk, mt.start_date, mt.end_date) * 2
      ) +(
        case when datename(dw, mt.start_date) = 'sunday' then 1 else 0 end
      ) +(
        case when datename(dw, mt.end_date) = 'saturday' then 1 else 0 end
      )
  ) as weekends, 
  case when datediff(day, mt.start_date, mt.end_date) -(
    select 
      (
        datediff(wk, mt.start_date, mt.end_date) * 2
      ) +(
        case when datename(dw, mt.start_date) = 'sunday' then 1 else 0 end
      ) +(
        case when datename(dw, mt.end_date) = 'saturday' then 1 else 0 end
      )
  ) -(
    select 
      count(*) 
    from 
      HolidayDates hd 
    where 
      hd.holydayDate between mt.start_date 
      and mt.end_date 
      and DATEPART(WEEKDAY, hd.holydayDate) between 2 
      and 6
  ) > 3 then 0 --> this need to exclude weekend and holidays
  when mt.code = 1 then 1 when mt.code = 2 then 2 else 3 end as mycolumn 
from 
  MyTable mt
total days  holydays (not weekends) weekends    mycolumn
----------- ----------------------- ----------- -----------
7           2                       2           3
61          2                       18          0
9           2                       2           0

(3 row(s) affected)