Calculating timespan with t-sql

前端 未结 7 1758
时光取名叫无心
时光取名叫无心 2020-11-30 07:32

Given two date/times:

@start_date = \'2009-04-15 10:24:00.000\'
@end_date = \'2009-04-16 19:43:01.000\'

Is it possible to calculate the tim

相关标签:
7条回答
  • 2020-11-30 07:39

    I know this thread is older and the original participants are likely no longer watching, but I stumbled upon it, and had already written some code fairly recently to do something very close to what jdiaz is requesting. The result is rendered as a string in D:H:M:S format.

    Step one would be to get the time span in seconds:

    DECLARE @ElapsedS INT
    SET @ElapsedS = DATEDIFF(second, @start_date, @end_date)
    

    Now create the following scalar function:

    CREATE FUNCTION [dbo].[udfTimeSpanFromSeconds]
    (
        @Seconds int
    )
    RETURNS varchar(15)
    AS
    BEGIN
    DECLARE 
        --Variable to hold our result
          @DHMS varchar(15)
        --Integers for doing the math
        , @Days int --Integer days
        , @Hours int --Integer hours
        , @Minutes int --Integer minutes
        --Strings for providing the display
        , @sDays varchar(5) --String days
        , @sHours varchar(2) --String hours
        , @sMinutes varchar(2) --String minutes
        , @sSeconds varchar(2) --String seconds
    
    --Get the values using modulos where appropriate
    SET @Hours = @Seconds/3600
    SET @Minutes = (@Seconds % 3600) /60
    SET @Seconds = (@Seconds % 3600) % 60
    
    --If we have 24 or more hours, split the @Hours value into days and hours
    IF @Hours > 23 
    BEGIN
        SET @Days = @Hours/24
        SET @Hours = (@Hours % 24)
    END
    ELSE
    BEGIN
        SET @Days = 0
    END
    
    --Now render the whole thing as string values for display
    SET @sDays = convert(varchar, @Days)
    SET @sHours = RIGHT('0' + convert(varchar, @Hours), 2)
    SET @sMinutes = RIGHT('0' + convert(varchar, @Minutes), 2)
    SET @sSeconds = RIGHT('0' + convert(varchar, @Seconds), 2)
    
    --Concatenate, concatenate, concatenate
    SET @DHMS =  @sDays + ':' + @sHours + ':' + @sMinutes + ':' + @sSeconds
    
    RETURN @DHMS
    
    END
    

    Now feed your timespan into the newly created function:

    SELECT TimeSpan = dbo.udfTimeSpanFromSeconds(@ElapsedS)
    

    Should produce '1:09:19:01'

    0 讨论(0)
  • 2020-11-30 07:41

    You can get the difference between the two dates to whatever resolution you want (in your example, minutes):

    DATEDIFF(minute, @start_date, @end_date)
    

    From there it's a simple matter of dividing minutes into hours and hours into days and modding the remainder.

    0 讨论(0)
  • 2020-11-30 07:41

    datediff(datepart, date1, date2);

    Rex's answer is more complete.

    0 讨论(0)
  • 2020-11-30 07:47
    DECLARE @FirstDate DATETIME, @SecondDate DATETIME, @result VARCHAR(MAX)
    SELECT @FirstDate = '2017-03-01 09:54:00.637', @SecondDate = GETDATE()
    
    DECLARE @Day INT,@Month INT,@Hour INT, @Minute INT,@TotalSeconds INT,@Year INT
    SELECT @TotalSeconds = ABS(DATEDIFF(SECOND,@FirstDate,@SecondDate))
    
    -- Standard values in seconds
    DECLARE @YearSeconds INT, @MonthSeconds INT, @DaySeconds INT, @HourSeconds INT, @MinuteSeconds INT
    
    SELECT  @MinuteSeconds = 60
    SELECT  @HourSeconds = 60 * @MinuteSeconds 
    SELECT  @DaySeconds = 24 * @HourSeconds
    SELECT  @MonthSeconds = 30 * @DaySeconds
    SELECT  @YearSeconds = 12 * @MonthSeconds
    
    --SELECT @MinuteSeconds AS [Minutes], @HourSeconds AS [Hours], @DaySeconds AS [Day],@MonthSeconds AS [Month],@YearSeconds AS [Year]
    
    IF @TotalSeconds < @MinuteSeconds
    BEGIN
        SELECT @result = CAST(@TotalSeconds AS NVARCHAR(20)) + ' seconds ago' 
    END
    ELSE IF @TotalSeconds < @HourSeconds
    BEGIN
        SELECT @result = CAST(ABS(DATEDIFF(MINUTE,@FirstDate,@SecondDate)) AS NVARCHAR(20)) + ' minutes ago' 
    END
    ELSE IF @TotalSeconds < @DaySeconds
    BEGIN
        SELECT @result = CAST(ABS(DATEDIFF(HOUR,@FirstDate,@SecondDate)) AS     NVARCHAR(20)) + ' hours ago' 
    END
    ELSE IF @TotalSeconds < @MonthSeconds
    BEGIN
        SELECT @result = CAST(ABS(DATEDIFF(DAY,@FirstDate,@SecondDate)) AS NVARCHAR(20)) + ' days ago' 
    END
    ELSE IF @TotalSeconds < @YearSeconds
    BEGIN
        SELECT @result = CAST(ABS(DATEDIFF(MONTH,@FirstDate,@SecondDate)) AS NVARCHAR(20)) + ' months ago' 
    END
    ELSE IF @TotalSeconds > @YearSeconds
    BEGIN
        SELECT @result = CAST(ABS(DATEDIFF(YEAR,@FirstDate,@SecondDate)) AS NVARCHAR(20)) + ' year ago' 
    END
    
    
    SELECT @result
    
    0 讨论(0)
  • 2020-11-30 07:59

    DATEDIFF can return unintuitive values. For example, the two dates below differ by one second yet DATEDIFF with the parameters below and interpreted as others have interpreted it above returns 1 year:

    SELECT DATEDIFF(year, '2005-12-31 23:59:59', '2006-01-01 00:00:00')

    Look at the MSDN documentation for DATEDIFF to understand how it works.

    0 讨论(0)
  • 2020-11-30 07:59

    Here's how you format the datediff (50d 8h 35m) in a query:

    Declare @Date1 as Datetime, @Date2 as Datetime
    Set @Date1 = '2005-01-01 08:00:00'
    Set @Date2 = '2005-02-20 16:35:30'
    
    Select
    CAST(DATEDIFF(Minute,@Date1, @Date2)/60/24 as Varchar(50)) ++ 'd ' ++ 
    CAST((DATEDIFF(Minute,@Date1, @Date2)/60)-((DATEDIFF(Minute,@Date1, @Date2)/60/24)*24) as Varchar(50)) ++ 'h ' ++
    CAST((DATEDIFF(Minute,@Date1, @Date2)) - (DATEDIFF(HOUR,@Date1, @Date2)*60) as Varchar(50)) ++ 'm' as FormattedDateDiff
    
    0 讨论(0)
提交回复
热议问题