代码之家  ›  专栏  ›  技术社区  ›  SP1

如何在SQL Server中获取两个日期之间的小时数

  •  0
  • SP1  · 技术社区  · 5 年前

    2019-01-02 12:33:36.000 2019-01-09 19:05:18.000 我想计算这两者之间的小时和分钟,不包括星期六和星期天。我能计算出差额,但不知道怎样才能把星期六和星期天排除在计算之外。

    我正在使用SQL Server 2008

    Edit -- From the comments suggestions I have this to calculate weekends
    ..DECLARE @StartDate DATETIME
    DECLARE @EndDate DATETIME
    SET @StartDate = '2019-01-02 12:33:36.000'
    SET @EndDate = '2019-01-09 19:05:18.000'
    
    SELECT
      (DATEDIFF(wk, @StartDate, @EndDate) * 2)
       +(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday'   THEN 1 ELSE 0 END)
       +(CASE WHEN DATENAME(dw, @EndDate)   = 'Saturday' THEN 1 ELSE 0 END) 
    

    现在我只计算分钟数,然后从总数中减去。

    1 回复  |  直到 5 年前
        1
  •  0
  •   SP1    5 年前

    所以在从大卫那里得到一些建议后,这里是我的最终代码,它可以让我在两个日期之间的分钟数,不包括周末

    DECLARE @StartDate DATETIME
    DECLARE @EndDate DATETIME
    SET @StartDate = '2019-03-07 00:00:00.000'
    SET @EndDate = '2019-03-11 23:59:59.000'
    
    Declare @TotalMins int
    Declare @Weekends int
    Declare @FinalMinutes int
    
    Set @TotalMins =  DATEDIFF(MINUTE, @StartDate,@EndDate);
    
    Set @Weekends = 
      (DATEDIFF(wk, @StartDate, @EndDate) * 2)
       +(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday'   THEN 1 ELSE 0 END)
       +(CASE WHEN DATENAME(dw, @EndDate)   = 'Saturday' THEN 1 ELSE 0 END)
    
    Set @FinalMinutes = @TotalMins -  (@Weekends * 24 * 60)
    Select @FinalMinutes
    

        2
  •  0
  •   tkeen    5 年前

    使用函数返回两个日期之间每分钟的日期/时间表,然后从结果中排除周末,并将计数转换为小时和分钟;

    DECLARE @StartDate DATETIME
    DECLARE @EndDate DATETIME
    SET @StartDate = '2019-03-07 00:00:00.000'
    SET @EndDate = '2019-03-11 23:59:59.000' 
    
    SELECT COUNT([Start_Date])/60 as 'Hours' 
    , COUNT([Start_Date])%60 as 'Minutes'
    FROM dbo.generateDateTable(@StartDate, @EndDate, 'minute',1)
    WHERE DATENAME(DW, [Start_Date]) NOT IN ('Saturday','Sunday')
    

    方便生成的可更新功能;

    CREATE function [dbo].[generateDateTable] 
    (
    @start_date datetime
    , @end_date datetime
    , @datepart varchar(20) = 'day'
    , @step int = 1
    )
    returns @dates table 
    (
    start_date datetime, 
    end_date datetime
    )
    as
    begin
    if( @datepart in ('year', 'yy', 'yyyy', 'quarter', 'qq', 'q', 'month', 'mm', 'm', 
    'dayofyear', 'dy', 'y', 'day', 'dd', 'd', 'week', 'wk', 'ww') )
    begin
        set @start_date = cast(floor(cast(@start_date as float)) as datetime)
        set @end_date = cast(floor(cast(@end_date as float)) as datetime)
    end
    
    declare @new_start datetime
    
    while @start_date <= @end_date
    begin
    set @new_start = (case 
        when @datepart in ('year', 'yy', 'yyyy') then dateadd(yy, @step, @start_date)
        when @datepart in ('quarter', 'qq', 'q') then dateadd(qq, @step, @start_date)
        when @datepart in ('month', 'mm', 'm') then dateadd(mm, @step, @start_date) 
        when @datepart in ('dayofyear', 'dy', 'y') then dateadd(dy, @step, 
        @start_date) 
        when @datepart in ('day', 'dd', 'd') then dateadd(dd, @step, @start_date) 
        when @datepart in ('week', 'wk', 'ww') then dateadd(ww, @step, @start_date) 
        when @datepart in ('hour', 'hh') then dateadd(hh, @step, @start_date) 
        when @datepart in ('minute', 'mi', 'n') then dateadd(n, @step, @start_date) 
        when @datepart in ('second', 'ss', 's') then dateadd(s, @step, @start_date) 
        when @datepart in ('millisecond', 'ms') then dateadd(ms, @step, @start_date) 
        else dateadd(dd, @step, @start_date)
    end)
        insert 
            @dates 
            (
              start_date
            , end_date
            ) values (
            @start_date
            , dateadd(ms, -3, @new_start)
            )
            set @start_date = @new_start
        end
        return 
    end