Convert Duration to Seconds in SQL

后端 未结 1 1044
深忆病人
深忆病人 2021-01-29 13:08

I have this table data in SQL which shows the user and the duration, but the duration is some times in pure seconds, while some is mixed between minutes and seconds

相关标签:
1条回答
  • 2021-01-29 13:50

    One method is to convert the string to a time type and then use time arithmetic:

    select datediff(second, 0, cast('00:' + right('0' + duration, 5) as time))
    

    Another method uses just string and number operations:

    select cast(left(duration, charindex(':', t) - 1) as int)*60 + cast(right(duration, 2) as int)
    
    0 讨论(0)
提交回复
热议问题