简体   繁体   English

如何找到两个日期范围之间的总时差

[英]How to find total time difference bwteen date range

I have created a procedure which returns time difference in minutes between 2 time ie ShiftStartTime, ShiftEndtime. 我创建了一个过程,该过程以2分钟为单位返回时差,即ShiftStartTime,ShiftEndtime。

Now problem is that if i pass 2 date ranges then how i will calculate total shiftTime between those 2 dates eg TimeDifference returns me 480 min for today but i want to find total ShiftDifference for 3 days or more, then how i will add TimeDifference for more than 1 day ? 现在的问题是,如果我通过2个日期范围,那么我将如何计算这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

I will calculate total shiftTime between those 2 dates 我将计算这两个日期之间的总shiftTime

You mention that you will calculate difference between two dates but in your code you declare the variables as time 您提到要计算两个日期之间的差,但是在代码中您将变量声明为time

Declare @ShiftStartTime time
Declare @ShiftEndTime time

Thus DATEDIFF(mi,@ShiftStartTime,@ShiftEndTime) only the difference between the times not the dates 因此DATEDIFF(mi,@ShiftStartTime,@ShiftEndTime)仅是时间之间的差,而不是日期

If you want to get the difference between dates you need use datetime datatype 如果要获取日期之间的差异,则需要使用datetime数据类型

On a side note, you have declared input parameters which have not been used in the proocedure as mentioned by Pradeep 附带说明一下,您已经声明了Pradeep提到的未在过程中使用的输入参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM