2013-02-05 54 views

回答

18

你可以使用DATEDIFF找到分鐘的差異,轉換到這時間:

select datediff(mi, startdate, enddate) 

假設1.5手段1小時30分鐘就可以簡單地除以60的結果:

select datediff(mi, startdate, enddate)/60.0 
2

它會幫助你....

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

請嘗試:

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'[email protected] +' Min' 
    else 
     select @[email protected] +' Min' 
    end 
    else 
    select '00 Hrs 00 Min' 
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) 
+5

'DATEDIFF'返回一個整數。因此它不會像分鐘那樣給出小數部分。 – Tobsey

0

得到真實的時差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