Count work days between two dates

前端 未结 24 2688
失恋的感觉
失恋的感觉 2020-11-22 01:05

How can I calculate the number of work days between two dates in SQL Server?

Monday to Friday and it must be T-SQL.

24条回答
  •  一向
    一向 (楼主)
    2020-11-22 01:57

    CREATE FUNCTION x
    (
        @StartDate DATETIME,
        @EndDate DATETIME
    )
    RETURNS INT
    AS
    BEGIN
        DECLARE @Teller INT
    
        SET @StartDate = DATEADD(dd,1,@StartDate)
    
        SET @Teller = 0
        IF DATEDIFF(dd,@StartDate,@EndDate) <= 0
        BEGIN
            SET @Teller = 0 
        END
        ELSE
        BEGIN
            WHILE
                DATEDIFF(dd,@StartDate,@EndDate) >= 0
            BEGIN
                IF DATEPART(dw,@StartDate) < 6
                BEGIN
                    SET @Teller = @Teller + 1
                END
                SET @StartDate = DATEADD(dd,1,@StartDate)
            END
        END
        RETURN @Teller
    END
    

提交回复
热议问题