如何查找MS SQL Server中不包括周末的2个日期之间的小时数

时间:2015-03-16 04:30:25

标签: sql sql-server

我需要找到2个日期之间的小时,不包括周末。如何在MS SQL Server中执行此操作?

我试过了,

CAST(( GETDATE() - [approval_date]) AS FLOAT) * 24.0 AS WorkedHours, 

包括周末。

4 个答案:

答案 0 :(得分:0)

这是旧文章,但可能会对某人有所帮助。该查询返回两个日期之间的确切天,月和小时,不包括所有星期日和星期六:

SELECT  (DATEDIFF(MINUTE, @d1, @d2) -  DATEDIFF(WK, @d1, @d2) * 2880) + 
CASE 
    WHEN  DATEDIFF(WK, @d1, @d2) = 1 AND DATEDIFF(DW, @d1, @d2) <= 5 THEN 0 
    WHEN  DATEDIFF(WK, @d1, @d2) = 0 THEN 0
ELSE 1440 END

SELECT (DATEDIFF(HOUR, @d1, @d2) - DATEDIFF(WK, @d1, @d2) * 48) + 
CASE 
    WHEN  DATEDIFF(WK, @d1, @d2) = 1 AND DATEDIFF(DW, @d1, @d2) <= 5 THEN 0  
    WHEN  DATEDIFF(WK, @d1, @d2) = 0 THEN 0
ELSE 24 END

SELECT (DATEDIFF(DW, @d1, @d2) - DATEDIFF(WK, @d1, @d2) * 2) + 
CASE 
    WHEN  DATEDIFF(WK, @d1, @d2) = 1 AND DATEDIFF(DW, @d1, @d2) <= 5 THEN 0
    WHEN  DATEDIFF(WK, @d1, @d2) = 0 THEN 0
ELSE 1 END

或者您可以使用以下通用功能:

CREATE FUNCTION [dbo].[fn_GetBusinnessDaysTimeSpan]( 
 @DateFrom DATETIME,
 @DateTO DATETIME,
 @Type VARCHAR(50)
) 
RETURNS BIGINT
AS 
BEGIN 
        DECLARE @result AS BIGINT;
        IF @Type = 'Min'
        BEGIN
            SET @result = (SELECT  (DATEDIFF(MINUTE, @DateFrom, @DateTO) -  DATEDIFF(WK, @DateFrom, @DateTO) * 2880) + 
                            CASE 
                                WHEN  DATEDIFF(WK, @DateFrom, @DateTO) = 1 AND (DATEDIFF(DW, @DateFrom, @DateTO)) <= 5 THEN 0 
                                WHEN  DATEDIFF(WK, @DateFrom, @DateTO) = 0 THEN 0 
                            ELSE 1440 END
                            )
        END

        IF @Type = 'Hour'
        BEGIN
            SET @result = (SELECT (DATEDIFF(HOUR, @DateFrom, @DateTO) - DATEDIFF(WK, @DateFrom, @DateTO) * 48) + 
                CASE 
                    WHEN  DATEDIFF(WK, @DateFrom, @DateTO) = 1 AND (DATEDIFF(DW, @DateFrom, @DateTO)) <=5 THEN 0  
                    WHEN  DATEDIFF(WK, @DateFrom, @DateTO) = 0 THEN 0 
                ELSE 24 END
                )
        END

        IF @Type = 'Day'
        BEGIN
            SET @result = (SELECT (DATEDIFF(DW, @DateFrom, @DateTO) - DATEDIFF(WK, @DateFrom, @DateTO) * 2) + 
                            CASE 
                                WHEN  DATEDIFF(WK, @DateFrom, @DateTO) = 1 AND (DATEDIFF(DW, @DateFrom, @DateTO)) <=5 THEN 0  
                                WHEN  DATEDIFF(WK, @DateFrom, @DateTO) = 0 THEN 0 
                            ELSE 1 END
                            )
        END
        RETURN @result 
END

答案 1 :(得分:0)

不幸的是,这并没有我想象的那么直接。

我以this博客/教程为基础,并从那里开始工作。

这是我现在使用的标量函数。我做了一些烟雾测试,看来效果很好。如果您发现任何错误,请告诉我,以便我可以根据需要测试和更新解决方案。我希望这对其他人有帮助!

ALTER FUNCTION [dbo].[GetWeekDayHoursDiff](@DateFrom datetime, @DateTo datetime)
RETURNS INT 

AS
BEGIN 

IF @DateFrom > @DateTo  
BEGIN
    return CAST('DateFrom cannot be lower thant DateTo parameters' as int);  -- https://www.youtube.com/watch?v=2ZIpFytCSVc
END 

DECLARE @TotHours INT= DATEDIFF(Hour, @DateFrom, @DateTo);
DECLARE @TotWeekHours INT= DATEDIFF(WEEK, @DateFrom, @DateTo) * 48;
DECLARE @SundayOffset INT= CASE
                     WHEN DATENAME(WEEKDAY, @DateFrom) = 'Sunday' THEN  24 - DATEPART(HOUR, @DateFrom)
                     WHEN DATENAME(WEEKDAY, @DateFrom) = 'Saturday' THEN - (DATEPART(HOUR, @DateFrom))
                     ELSE 0
                  END;
DECLARE @SaturdayOffset INT= CASE
                       WHEN DATENAME(WEEKDAY, @DateTo) = 'Saturday' THEN - DATEPART(HOUR, @DateTo)
                       WHEN DATENAME(WEEKDAY, @DateTo) = 'Sunday' THEN  - DATEPART(HOUR, @DateTo) + 24
                       ELSE 0
                    END;


DECLARE @TotWorkingHours INT= @TotHours - @TotWeekHours - @SundayOffset + @SaturdayOffset;

   -- select @TotWorkingHours TotWorkingHours
      --   , @TotHours TotHours
         --, @TotWeekHours TotWWeekHours
         --, @SundayOffset SundayOffset
         --, @SaturdayOffset SaturdayOffset

RETURN @TotWorkingHours


END

答案 2 :(得分:-1)

试试这个

select datediff(hh, @d1, @d2)

其中@ d1 = date1 和@ d2 = date2

在你的情况下,它就像这样

 select datediff(hh, GETDATE(), [approval_date])

答案 3 :(得分:-2)

根据get DATEDIFF excluding weekends using sql server,按照此查询获取时间

SELECT DATEDIFF(HH, @date1, @date2) - (DATEDIFF(WK, @date1, @date2) * 2) -
       CASE WHEN datepart(DW, @d1) = 1 THEN 24 ELSE 0 END +
       CASE WHEN datepart(DW, @d2) = 1 THEN 24 ELSE 0 END