如何查找日期范围之间的总时差

时间:2014-10-18 06:29:13

标签: sql tsql

我创建了一个程序,它返回2个时间间隔的时差,即ShiftStartTime,ShiftEndtime。

现在的问题是,如果我通过2个日期范围,那么我将如何计算这两个日期之间的总shiftTime,例如TimeDifference今天给我480分钟,但是我想找到3天或更长时间的总ShiftDifference,然后我将如何添加TimeDifference超过1天?

CREATE PROCEDURE GetShiftTotalDuration

 @ShiftID int = 9,
 @FromDate DateTime,
 @ToDate DateTime

AS
BEGIN

    Declare @TimeDifference int
    Declare @ShiftStartTime time
    Declare @ShiftEndTime time

    Set @ShiftStartTime = (Select DeparmentShiftsHistory.StartTime from DeparmentShiftsHistory 
                           where DeparmentShiftsHistory.Shift_ID= 9)
    Set @ShiftEndTime = (Select DeparmentShiftsHistory.EndTime from DeparmentShiftsHistory
                           where DeparmentShiftsHistory.Shift_ID= 9)
    Set @TimeDifference = (Select DATEDIFF(mi,@ShiftStartTime,@ShiftEndTime)) --Returns time difference in seconds



END
GO

1 个答案:

答案 0 :(得分:0)

  

我将计算这两个日期之间的总shiftTime

您提到您将计算两个日期之间的差异,但在您的代码中,您将变量声明为time

Declare @ShiftStartTime time
Declare @ShiftEndTime time

因此DATEDIFF(mi,@ShiftStartTime,@ShiftEndTime)只是时间之间的差异而不是日期

如果您希望获得日期之间的差异,则需要使用datetime数据类型

在旁注中,您已声明了Pradeep

中提到的尚未在程序中使用的输入参数
相关问题