Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Date format

Author  Topic 

Irfan.abdullah
Starting Member

9 Posts

Posted - 2012-09-05 : 19:44:24
Hi,

I have a date field that is formated to show data in hrs.minutes.second

How do I calculate average on this please

Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-05 : 20:43:44
[code]
SELECT CONVERT(varchar(8), DATEADD(ss,AVG(datediff(ss,0,datefield)*1.0),0),108)
FROM table
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Irfan.abdullah
Starting Member

9 Posts

Posted - 2012-09-06 : 19:45:33
Thanks for reply can you please explain in more detail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-08 : 17:10:57
try it out on sample dataset and you will understand how it returns you average

datediff will return seconds elapsed since base date ie time value
taking average over it gives average time
again adding it to 0 will make it date value which when converted to string using 108 gives you result in hh:mm:ss format

see

http://visakhm.blogspot.com/2012/07/generate-datetime-values-from-integers.html



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -