The average for Date or Time in T-SQL

Categories

The average for Date or Time in T-SQL:

We cannot use the average function (AVG) to average dates or times or date-times. I think the best way for this is to convert the date/time to float with Convert(Float,DateField). This function will return the number of days between the DateField and ‘۱۹۰۰-۰۱-۰۱ ۰۰:۰۰:۰۰’. For example it will return 0.5 for ‘۱۹۰۰-۰۱-۰۱ ۱۲:۰۰:۰۰’ so if you want just the days number you can use convert(int,DateField).

So the average will be like this:

Select id, Avg(convert(int,DateField)) from MyTable