Convert HH:MM:SS string to number of minutes

后端 未结 8 616
醉梦人生
醉梦人生 2021-01-22 12:03

I have the below query.

select cast(dateadd(minute, datediff(minute, TimeIn, TimeOut), 0) as time(0) )

I get the results from two columns in th

8条回答
  •  [愿得一人]
    2021-01-22 12:56

    Expanding on Justin's answer. This allows for situations where hours is larger than 2 digits.

    declare @time varchar(50) = '102:47:05'
    
    SELECT cast(right(@time,2) AS int)+
           cast(left(right(@time,5),2) AS int)*60+ 
           cast(left(@time,len(@time)-6) AS int)*3600 AS seconds,
           (cast(right(@time,2) AS int)+
           cast(left(right(@time,5),2) AS int)*60+ 
           cast(left(@time,len(@time)-6) AS int)*3600)/60.0 AS minutes
    

    Result:

    seconds     minutes
    ----------- ---------------------------------------
    370025      6167.083333
    

提交回复
热议问题