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

时间:2019-03-07 21:05:57

标签: sql sql-server sql-server-2008

我有两个日期2019-01-02 12:33:36.0002019-01-09 19:05:18.000 我想计算两者之间的小时和分钟,不包括周六和周日。我可以计算出差额,但不确定如何从计算中排除周六和周日。

没有工作时间会被排除,只是星期六到星期日的凌晨12点到下午12点需要排除,而我正在使用SQL Server 2008。

我正在使用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) 

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

2 个答案:

答案 0 :(得分:0)

因此,在获得David的一些建议之后,这是我的最终代码,该代码可以使我获得两个日期之间(除了周末)的分钟数

map_data("state", region = c("north dakota", "south dakota"), boundary = TRUE, interior = FALSE)

谢谢

答案 1 :(得分:0)

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

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

便捷的GenerateDateTable函数;

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