Calculate the SUM of the Column which has Time DataType:

前端 未结 5 1158
无人及你
无人及你 2021-01-17 04:37

I want to calculate the Sum of the Field which has Time DataType.

My Table is Below:

  TableA:

            TotalTime
          -------------
                


        
5条回答
  •  佛祖请我去吃肉
    2021-01-17 05:26

    You could sum the total number of seconds, or datediff(second,0,datecolumn). You can format that as a time string with some math. For example, the total number of minutes is totalseconds / 60 % 60. For example:

    select  cast(sum(datediff(second,0,dt))/3600 as varchar(12)) + ':' + 
            right('0' + cast(sum(datediff(second,0,dt))/60%60 as varchar(2)),2) +
            ':' + right('0' + cast(sum(datediff(second,0,dt))%60 as varchar(2)),2)
    from    TestTable
    

    Working code at SQL Fiddle.

提交回复
热议问题