Operand data type time is invalid for avg operator...? Operand data type time is invalid for avg operator...? sql sql

Operand data type time is invalid for avg operator...?


You can use DateDiff( ms, '00:00:00', e.Duration ) to convert the time into an integer number of milliseconds. Use that for your aggregate, then convert the result back, e.g. Cast( DateAdd( ms, 1234, '00:00:00' ) as Time ).


Improving off of HABO answer:

select top 10Cast(DateAdd( ms,avg(DateDiff( ms, '00:00:00', e.Duration)), '00:00:00' )  as time) as 'avg duration' from TimeTable e


Addition to HABO's and Rafi's answers.

For my case, I had to cast the value of the DATEDIFF to a bigint because my value grew too large and caused an arithmetic overflow error.

CAST(DATEADD( ms,AVG(CAST(DATEDIFF( ms, '00:00:00', ISNULL(e.Duration, '00:00:00')) as bigint)), '00:00:00' )  as TIME) as 'avg_time'