How to get hours between two dates in SQL Server?

2019-08-25 04:51发布

I have two dates 2019-01-02 12:33:36.000 and 2019-01-09 19:05:18.000 I want to calculate the hours and mins between the two excluding Saturday and Sunday. I can calculate the difference but not sure how I can exclude Saturday and Sunday from the calculation.

There is no working hours to be excluded just 12AM to 12PM Saturday to Sunday needs to be excluded and I am using SQL Server 2008.

I am using 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) 

Now I will just calculate the mins and Subtract from total.

2条回答
你好瞎i
2楼-- · 2019-08-25 05:37

Use a function to return a table of date/times for each minute between the 2 dates then exclude the weekends from the result and convert the count into hours and minutes;

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')

Handy GenerateDateTable function;

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
查看更多
迷人小祖宗
3楼-- · 2019-08-25 05:38

So after getting some suggestions from David here is my final code which gets me the mins between two dates excluding weekends

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

Thanks

查看更多
登录 后发表回答