I posted an answer last night, but I deleted it, since the answer was off the mark, because I did not read the question well enough.
Let me first say that this is not good way of storing time. Preferably, you should use any of the built-in data types for date and time. Then again, SQL Server does not have an interval type, so there is no perfect solution. But I would prefer to store the elapsed time as an integer value with the number of minutes. Formatting can always be done later. With your current setup, there is a considerable risk that illegal values sneak into the table.
Anyway, here is a query. It first replaces the :
with .
, so the string can be split with the parsename
function. Then it is straightforward to convert all to minutes and make the sum. The final SELECT performs the formatting (although, this may better be done in the presentation layer). I'm using the str
function which produces a right-adjusted string of a numeric value. The second argument specifies the width we want. With replace
, I replace the space with a 0
. Note here that /
is integer division, since both values are integer. %
is the modulus operator.
; WITH first_replace AS (
SELECT replace(Elapsed_Time, ':', '.') AS T
FROM Test
)
, parts AS (
SELECT convert(int, parsename(T, 2)) AS hours,
convert(int, parsename(T, 1)) AS minutes
FROM first_replace
)
, dothesum AS (
SELECT SUM(60*hours + minutes) AS total
FROM parts
)
SELECT str(total / 60, 5) + ':' + replace(str(total % 60, 2), ' ', '0')
FROM dothesum
go
DROP TABLE Test