在SQL中的确切时间数两个日期之间的差异(Difference between two dates

2019-08-16 18:18发布

我想计算两个日期时间变量之间的精确时间差。 该时间差应确切是这样的:

1.5
2
6.25

任何人都请帮助out..Thanks提前...

Answer 1:

你可以使用DATEDIFF找到分钟的差异,转换到这时间:

select datediff(mi, startdate, enddate)

假定1.5意味着1小时30分钟就可以简单地除以60的结果:

select datediff(mi, startdate, enddate) / 60.0


Answer 2:

保持简单,儿子。

declare @date1 datetime
declare @date2 datetime

select @date1 = GETDATE();
select @date2 = '2013-02-02 14:05'

select DATEDIFF(hh, @date2, @date1)


Results
-----------
71

(1 row(s) affected)


Answer 3:

它会帮助你....

  Declare @Date1 dateTime
  Declare @Date2 dateTime 
  Set @Date1 = '22:30:00'   
  Set @Date2 = '00:00:00'
  Select Cast((@Date1 - @Date2) as Float) * 24.0


Answer 4:

请试试:

declare @dt1 datetime, @dt2 datetime, @Seconds int
select @dt1='2013-02-05 14:05:55.113', @dt2 =getdate()
set @Seconds=datediff(second, @dt1, @dt2)

    declare @Hour nvarchar(50)
    declare @Min nvarchar(50)
    declare @MinTemp int        
    if @Seconds >0 
    begin
    set @Hour=cast((@Seconds / 3600) as nvarchar(20)) +' Hrs '
    set @MinTemp= (@Seconds % 3600) / 60
    set @Min=cast(@MinTemp as nvarchar(20))
    if  @MinTemp<10 
        select @Hour+'0'+@Min +' Min'
    else
        select @Hour+@Min +' Min'
    end
    else 
    select '00 Hrs 00 Min'


Answer 5:

为了获得精确的HH时差:MM尝试在MS-SQL下面的代码

 Declare @InTime datetime='2017-11-27 10:00:00',
 @OutTime datetime='2017-11-27 11:15:00'

 SELECT CONVERT(varchar(5),DATEADD(minute,DATEDIFF(minute,@InTime,@OutTime),0), 114)
 -----------
 Result
 01:15


文章来源: Difference between two dates in exact number of hours in SQL