且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算SQL Server中两个日期之间的工作时间(不包括周末)

更新时间:2023-02-05 12:32:21

如果您首先创建一个日历表(每个日期一行),则要做起来会容易得多.然后,您可以使用该服务过滤掉周末,然后在预订有效的每一天都会得到一行结果.

It's going to be a lot simpler to do this if you first create a calendar table, with one row per date. You can use that then to filter out weekends, and you will get one row as a result for each of the days when the booking is valid.

之后,您只需要检查:

  • 如果开始日期与日历日期相同->检查可能要从开始日期中扣除的小时数
  • 如果结束日期与日历日期相同->检查可能要从结束日期中扣除的小时数

然后将所有其他天加起来8个小时(如果那是您的工作日).

Then just sum all other days with 8 hours, if that was your working day.