Skip to content
Advertisement

How to Convert Hours, minutes and seconds (HH:mm:ss) to minutes and seconds (mm:ss) in SQL

Suppose I have 2hrs, 47minutes and 10 second (02:47:10) which is in hh:mm:ss format as a value in Time column. I would like to convert it to minutes and seconds only (mm:ss). Hence the result should be 167minutes and 10 secs in sql. I have tried using the code

SELECT cast(substring('02:47:10',1,2) AS int)*60+
       cast(substring('02:47:10',4,2) AS int)+ 
       cast(substring('02:47:10',7,2) AS int)/60.0 AS minutes

The result was 167.166666.

My desired output should be 167:10 (mm:ss) format

Advertisement

Answer

Perhaps a lighter approach with less string manipulation

Example

Declare @T time = '02:47:10'

Select concat(datediff(MINUTE,0,@T),':',datepart(second,@T))

Results

167:10
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement