SQL中确切小时数的两个日期之间的差异

时间:2022-04-11 10:16:26

I would like to calculate the exact hours difference between two datetime variables. The hours difference should be exact like this:

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

1.5
2
6.25

Anybody please help out..Thanks in advance...

有人请帮忙..谢谢你...

5 个解决方案

#1


21  

You could use DATEDIFF to find the difference in minutes and convert that into hours:

您可以使用DATEDIFF查找分钟差异并将其转换为小时:

select datediff(mi, startdate, enddate)

Assuming 1.5 means 1 hour and 30 minutes you could simply divide the result by 60:

假设1.5表示1小时30分钟,您可以简单地将结果除以60:

select datediff(mi, startdate, enddate) / 60.0

#2


3  

it will help you....

它会帮助你......

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

#3


3  

Keep it simple, son.

保持简单,儿子。

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)

#4


1  

Please try:

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'

#5


0  

To get Exact Time Difference in HH:MM try the below code in MS-SQL

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

#1


21  

You could use DATEDIFF to find the difference in minutes and convert that into hours:

您可以使用DATEDIFF查找分钟差异并将其转换为小时:

select datediff(mi, startdate, enddate)

Assuming 1.5 means 1 hour and 30 minutes you could simply divide the result by 60:

假设1.5表示1小时30分钟,您可以简单地将结果除以60:

select datediff(mi, startdate, enddate) / 60.0

#2


3  

it will help you....

它会帮助你......

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

#3


3  

Keep it simple, son.

保持简单,儿子。

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)

#4


1  

Please try:

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'

#5


0  

To get Exact Time Difference in HH:MM try the below code in MS-SQL

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