用t-sql计算时间跨度

给定两个date/时间:

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

是否可以按以下格式计算两个date之间的时间间隔?

1天9小时19分

你可以得到两个date之间的差异,以你想要的任何决议(在你的例子中,分钟):

 DATEDIFF(minute, @start_date, @end_date) 

从那里可以简单地将分钟分成几小时几小时,然后修改余数。

我知道这个线程比较老,原来的参与者很可能不再看,但是我偶然发现了这个线程,最近已经写了一些代码来做一些非常接近jdiaz请求的代码。 结果以D:H:M:S格式呈现为string。

第一步是获得时间跨度在几秒钟内:

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

现在创build下面的标量函数:

 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 

现在将您的时间跨度input到新创build的函数中:

 SELECT TimeSpan = dbo.udfTimeSpanFromSeconds(@ElapsedS) 

应该产生“1:09:19:01”

 CONVERT(varchar,(@end_date-@start_date),108) 

这将给你的HH:MM:SS

干杯

DATEDIFF可能返回不直观的值。 例如,下面的两个date相差一秒,但DATEDIFF与下面的参数,并解释为其他人解释它上面返回1年:

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

查看DATEDIFF的MSDN文档以了解它是如何工作的。

datediff(datepart,date1,date2);

雷克斯的答案更完整。

以下是如何在查询中对datediff(50d 8h 35m)进行格式化:

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