返回T-SQL中两个日期之间的工作日数

时间:2011-06-21 12:31:24

标签: sql sql-server tsql

我有两个约会,想知道有多少个工作日(周一至周五)

e.g。

thu jan 1 20xx    
fri jan 2 20xx    
sat jan 3 20xx    
sun jan 4 20xx    
mon jan 5 20xx  

1月1日,jan 5将返回3

(可以忽略公众假期)

6 个答案:

答案 0 :(得分:1)

尝试

 DateDiff(day, @DtA, @DtB) - 2 * DateDiff(Week, @DtA, @DtB)

这可能无法正常工作,但你可以看到这个想法。稍作修改即可。

答案 1 :(得分:1)

试试这个:

SET DATEFIRST 1
DECLARE @StartDate datetime
       ,@EndDate datetime
SELECT @StartDate='6/21/2011'
      ,@EndDate='6/28/2011'
;with AllDates AS
(
    SELECT @StartDate AS DateOf, datepart(weekday,getdate()) AS WeekDayNumber
    UNION ALL
    SELECT DateOf+1, datepart(weekday,DateOf+1)
        FROM AllDates
    WHERE DateOf<@EndDate
)
SELECT COUNT(*) AS WeekDayCount FROM AllDates WHERE WeekDayNumber<=5

输出:

WeekDayCount
------------
6

(1 row(s) affected)

如果您有假期表,您可以加入并删除它们。

编辑基于@Ross Watson评论:

SET DATEFIRST 1
DECLARE @StartDate datetime
       ,@EndDate datetime
SELECT @StartDate='6/21/2011'
      ,@EndDate='6/28/2011'
;with AllDates AS
(
    SELECT @StartDate AS DateOf, datepart(weekday,getdate()) AS WeekDayNumber
    UNION ALL
    SELECT DateOf+1, (WeekDayNumber+1) % 7
        FROM AllDates
    WHERE DateOf<@EndDate
)
SELECT COUNT(*) AS WeekDayCount FROM AllDates WHERE WeekDayNumber>0 AND WeekDayNumber<6
--I don't like using "BETWEEN", ">", ">=", "<", and "<=" are more explicit in defining end points

产生与原始查询相同的输出。

答案 2 :(得分:1)

假设相关日期不能超过五年半(或使用您自己的tally table代替master..spt_values):

DECLARE @date1 datetime, @date2 datetime;
SET @date1 = '20110901';
SET @date2 = '20110905';

SELECT COUNT(*)
FROM (
  SELECT
    Date = DATEADD(day, number, @date1)
  FROM master..spt_values
  WHERE type = 'P'
    AND number between 0 AND DATEDIFF(day, @date1, @date2)
) s
WHERE DATENAME(DW, Date) NOT IN ('Saturday', 'Sunday')

答案 3 :(得分:0)

尝试以下方法:

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2011/06/01'
SET @EndDate = '2011/06/31'

SELECT   
    (DATEDIFF(dd, @StartDate, @EndDate) + 1)  - 
    (DATEDIFF(wk, @StartDate, @EndDate) * 5)  -
    (
        CASE 
           WHEN DATENAME(dw, @StartDate) in 
               ('Sunday', 'Tuesday', 'Wednesday', 'Turesday', 'Saturday') 
           THEN 1 
           ELSE 0 
        END
    )  -
    (
        CASE 
           WHEN DATENAME(dw, @EndDate) in 
               ('Sunday', 'Tuesday', 'Wednesday', 'Turesday', 'Saturday') 
           THEN 1 
           ELSE 0 
        END
    )

答案 4 :(得分:0)

答案 5 :(得分:0)

由于递归,此方法仅限于约100天。这适用于我测试过的日期范围。同样的想法,删除了数学和简化:

BEGIN
    SET DATEFIRST 1
    DECLARE @StartDate datetime
           ,@EndDate datetime
    SELECT @StartDate='12/16/2015'
          ,@EndDate='1/8/2016'
    ;with AllDates AS
    (
        SELECT @StartDate AS DateOf 
        UNION ALL
        SELECT DateOf+1 
            FROM AllDates
        WHERE DateOf<@EndDate
    )
    SELECT COUNT(*) AS WeekDayCount 
        FROM 
        AllDates 
    WHERE 
        datepart(weekday,DateOf) between 1 AND 5

    --SELECT DateOf [date], datepart(weekday,DateOf) [day] 
    --FROM 
    --  AllDates 
    --WHERE 
    --  datepart(weekday,DateOf) between 1 AND 5
END