两个日期之间的SQL时差导致hh:mm:ss

时间:2012-11-27 05:26:44

标签: sql-server tsql

我在计算两个日期之间的时差方面遇到了一些困难。

我想要的是,我有两个约会,比如说

@StartDate = '10/01/2012 08:40:18.000'
@EndDate='10/04/2012 09:52:48.000'

因此hh:mm:ss形式的两个日期之间的差异为72:42:30

如何在T-SQL查询中获得此结果?

14 个答案:

答案 0 :(得分:40)

declare @StartDate datetime, @EndDate datetime

select @StartDate = '10/01/2012 08:40:18.000',@EndDate='10/04/2012 09:52:48.000'

select convert(varchar(5),DateDiff(s, @startDate, @EndDate)/3600)+':'+convert(varchar(5),DateDiff(s, @startDate, @EndDate)%3600/60)+':'+convert(varchar(5),(DateDiff(s, @startDate, @EndDate)%60)) as [hh:mm:ss]

此查询对您有所帮助。

答案 1 :(得分:24)

最短的代码是:

Select CAST((@EndDateTime-@StartDateTime) as time(0)) '[hh:mm:ss]'

答案 2 :(得分:9)

虽然可能不是最有效的,但这可行:

declare @StartDate datetime, @EndDate datetime

select @StartDate = '10/01/2012 08:40:18.000',@EndDate='10/04/2012 09:52:48.000'

select convert(varchar(5),DateDiff(s, @startDate, @EndDate)/3600)+':'+convert(varchar(5),DateDiff(s, @startDate, @EndDate)%3600/60)+':'+convert(varchar(5),(DateDiff(s, @startDate, @EndDate)%60))

如果你可以运行两个选择,那么这会更好,因为你只做了一次的约会:

declare @StartDate datetime, @EndDate datetime

select @StartDate = '10/01/2012 08:40:18.000',@EndDate='10/04/2012 09:52:48.000'
declare @Sec BIGINT

select @Sec = DateDiff(s, @startDate, @EndDate)

select convert(varchar(5),@sec/3600)+':'+convert(varchar(5),@sec%3600/60)+':'+convert(varchar(5),(@sec%60))

答案 3 :(得分:4)

如果您不反对隐式类型转换,我将提供另一种解决方案。更好的格式化是否更具可读性?你是法官。

DECLARE  @StartDate datetime = '10/01/2012 08:40:18.000'
        ,@EndDate   datetime = '10/04/2012 09:52:48.000'

SELECT
    STR(ss/3600, 5) + ':' + RIGHT('0' + LTRIM(ss%3600/60), 2) + ':' + RIGHT('0' + LTRIM(ss%60), 2) AS [hh:mm:ss]
FROM (VALUES(DATEDIFF(s, @StartDate, @EndDate))) seconds (ss)

答案 4 :(得分:4)

我喜欢将它变成一个函数的想法,因此它变得可以重复使用,并且您的查询变得更容易阅读:

--get the difference between two datetimes in the format: 'h:m:s'
CREATE FUNCTION getDateDiff(@startDate DATETIME, @endDate DATETIME)
RETURNS VARCHAR(10)
AS BEGIN
    DECLARE @seconds INT = DATEDIFF(s, @startDate, @endDate)
    DECLARE @difference VARCHAR(10) =
    CONVERT(VARCHAR(4), @seconds / 3600) + ':' +
    CONVERT(VARCHAR(2), @seconds % 3600 / 60) + ':' +
    CONVERT(VARCHAR(2), @seconds % 60)
    RETURN @difference
END

用法:

DECLARE @StartDate DATETIME = '10/01/2012 08:40:18.000'
DECLARE @endDate DATETIME = '10/04/2012 09:52:48.000'

SELECT dbo.getDateDiff(@startDate, @endDate) AS DateDifference

结果:

    DateDifference
1   73:12:30

如果添加填充,则读取结果也更容易,因此格式始终为hh:mm:ss。例如,以下是在SQL Server 2012或更高版本中执行此操作的方法:

--get the difference between two datetimes in the format: 'hh:mm:ss'
CREATE FUNCTION getDateDiff(@startDate DATETIME, @endDate DATETIME)
RETURNS VARCHAR(10)
AS BEGIN
    DECLARE @seconds INT = DATEDIFF(s, @startDate, @endDate)
    DECLARE @difference VARCHAR(10) =
    FORMAT(@seconds / 3600, '00') + ':' +
    FORMAT(@seconds % 3600 / 60, '00') + ':' +
    FORMAT(@seconds % 60, '00')
    RETURN @difference
END

请注意,如果长度超过2位,则不会剪切小时。因此,1小时将显示为01:00:00,100小时将显示为100:00:00

答案 5 :(得分:2)

DECLARE @dt1 datetime='2012/06/13 08:11:12', @dt2 datetime='2012/06/12 02:11:12'

SELECT CAST((@dt2-@dt1) as time(0))

答案 6 :(得分:1)

试试这个:

declare @StartDate datetime, @EndDate datetime

select @StartDate = '2016-05-04 10:23:41.083', @EndDate='2016-05-04 10:25:26.053'

select CAST(DateDiff(MI, @startDate, @EndDate)/60 AS varchar)+':'+Cast(DateDiff(MI, @startDate, @EndDate)%60 AS varchar)+':'+cast(DateDiff(s, @startDate, @EndDate)%60 AS varchar) as [hh:mm:ss]

答案 7 :(得分:1)

如果您需要两个日期之间的零填充差值:

SELECT convert(varchar(2),FORMAT(DATEDIFF(s, @startDate, @endDate)/3600,'0#'))+':'    
   +convert(varchar(2),FORMAT(DATEDIFF(s, @startDate, @endDate)%3600/60,'0#'))+':'
   +convert(varchar(2),FORMAT(DATEDIFF(s, @startDate, @endDate)%60,'0#')) AS Duration

答案 8 :(得分:0)

看看这些。我没有使用更多的括号来保持它的可读性,所以请记住乘法是在加法或减法之前完成的。

以下都返回:

hr  mins  sec   timediff
73   12    30   73:12:30

这是为了不使用子查询而且最易读和易懂:

declare @StartDate datetime,
@EndDate datetime

set @StartDate = '10/01/2012 08:40:18.000'
set @EndDate =   '10/04/2012 09:52:48.000' 

select datediff(hour, @StartDate, @EndDate) hr,
   datediff(minute, @StartDate, @EndDate) 
   - datediff(hour, @StartDate, @EndDate) * 60 mins,
   datediff(second, @StartDate, @EndDate) 
   - (datediff(minute, @StartDate, @EndDate) * 60) sec,
   cast(datediff(hour, @StartDate, @EndDate) as varchar)+':'+ 
   cast(datediff(minute, @StartDate, @EndDate) 
   - datediff(hour, @StartDate, @EndDate) * 60 as varchar)+':'+ 
   cast(datediff(second, @StartDate, @EndDate) 
   - (datediff(minute, @StartDate, @EndDate) * 60) as varchar) timediff

如果你有大量数据,这个版本会更好。它需要一个子查询。

declare @StartDate datetime,
@EndDate datetime

set @StartDate = '10/01/2012 08:40:18.000'
set @EndDate =   '10/04/2012 09:52:48.000' 

select s.seconds / 3600  hrs,
s.seconds / 60 - (seconds / 3600 ) * 60 mins,
s.seconds - (s.seconds / 60) * 60   seconds,
cast(s.seconds / 3600 as varchar) + ':' +
cast((s.seconds / 60 - (seconds / 3600 ) * 60) as varchar) + ':' +
cast((s.seconds - (s.seconds / 60) * 60) as varchar) timediff
from (select datediff(second, @StartDate, @EndDate) as seconds) s

答案 9 :(得分:0)

我今天遇到这篇文章,因为我试图收集位于关键字段上连接在一起的单独表格中的字段之间的时差。这是这种努力的工作代码。 (在sql 2010中测试) 请记住,我的原始查询在一个公共密钥字段上共同加入了6个表,在下面的代码中我删除了其他表,以免给读者造成任何混淆。

查询的目的是计算变量CreatedUTC& BackupUTC,其中差异以天表示,该字段称为'DaysActive。'

declare @CreatedUTC datetime
declare @BackupUtc datetime


SELECT TOP 500

table02.Column_CreatedUTC AS DeviceCreated,
CAST(DATEDIFF(day, table02.Column_CreatedUTC, table03.Column_EndDateUTC) AS nvarchar(5))+ ' Days' As DaysActive,
table03.Column_EndDateUTC AS LastCompleteBackup

FROM

Operations.table01 AS table01

LEFT OUTER JOIN

    dbo.table02 AS table02
ON
    table02.Column_KeyField = table01.Column_KeyField

LEFT OUTER JOIN 

    dbo.table03 AS table03
ON
    table01.Column_KeyField = table03.Column_KeyField

Where table03.Column_EndDateUTC > dateadd(hour, -24, getutcdate()) --Gathers records with an end date in the last 24 hours
AND table02.[Column_CreatedUTC] = COALESCE(@CreatedUTC, table02.[Column_CreatedUTC])
AND table03.[Column_EndDateUTC] = COALESCE(@BackupUTC, table03.[Column_EndDateUTC])

GROUP BY table03.Column_EndDateUTC, table02.Column_CreatedUTC
ORDER BY table02.Column_CreatedUTC ASC, DaysActive, table03.Column_EndDateUTC DESC

输出如下:

[DeviceCreated]..[DaysActive]..[LastCompleteBackup]
---------------------------------------------------------
[2/13/12 16:04]..[463 Days]....[5/21/13 12:14]
[2/12/13 22:37]..[97 Days].....[5/20/13 22:10]

答案 10 :(得分:0)

这是一个脚本写入副本然后写入您的脚本文件并进行更改  你的重新征领的领域并且得出结论

DECLARE @Sdate DATETIME, @Edate DATETIME, @Timediff VARCHAR(100)
SELECT @Sdate = '02/12/2014 08:40:18.000',@Edate='02/13/2014 09:52:48.000'
SET @Timediff=DATEDIFF(s, @Sdate, @Edate)
SELECT CONVERT(VARCHAR(5),@Timediff/3600)+':'+convert(varchar(5),@Timediff%3600/60)+':'+convert(varchar(5),@Timediff%60) AS TimeDiff

答案 11 :(得分:0)

DECLARE  @StartDate datetime = '10/01/2012 08:40:18.000'
        ,@EndDate   datetime = '10/10/2012 09:52:48.000'
        ,@DaysDifferent int = 0
        ,@Sec BIGINT

select @Sec = DateDiff(s, @StartDate, @EndDate)

IF (DATEDIFF(day, @StartDate, @EndDate) > 0)
    BEGIN
        select @DaysDifferent = DATEDIFF(day, @StartDate, @EndDate)
        select @Sec = @Sec - ( @DaysDifferent * 86400 )
        SELECT LTRIM(STR(@DaysDifferent,3)) +'d '+ LTRIM(STR(@Sec/3600, 5)) + ':' + RIGHT('0' + LTRIM(@Sec%3600/60), 2) + ':' + RIGHT('0' + LTRIM(@Sec%60), 2) AS [dd hh:mm:ss]
    END
ELSE
    BEGIN
        SELECT LTRIM(STR(@DaysDifferent,3)) +'d '+ LTRIM(STR(@Sec/3600, 5)) + ':' + RIGHT('0' + LTRIM(@Sec%3600/60), 2) + ':' + RIGHT('0' + LTRIM(@Sec%60), 2) AS [dd hh:mm:ss]
    END

----------------------------------------------------------------------------------
dd HH:MM:SS
9d 1:12:30

答案 12 :(得分:-1)

声明@StartDate日期时间, @EndDate datetime

set @StartDate = '10/01/2012 08:40:18.000'
set @EndDate =   '10/04/2012 09:52:48.000'

SELECT CONVERT(CHAR(8), CAST(CONVERT(varchar(23),@EndDate,121) AS DATETIME)
-CAST(CONVERT(varchar(23),@StartDate,121)AS DATETIME),8) AS TimeDiff

答案 13 :(得分:-1)

declare @StartDate datetime;
declare @EndDate datetime;
select @StartDate = '10/01/2012 08:40:18.000';
select @EndDate='10/04/2012 09:52:48.000';
select  cast(datediff(hour,@StartDate,@EndDate) as varchar(10)) + left(right(cast(cast(cast((@EndDate-@StartDate) as datetime) as time) as varchar(16)),14),6)