return a double from integer divide?

  • I'm doing a calculation in a select from which I need a double returned:

    SELECT SUM( RiderCount)/Count(*) as RiderAvg

    but RiderAvg is returned as a whole number. Is there a function or other method I can use to get this result?

    Thanks much.

  • SELECT SUM(CONVERT(double, RiderCount)) / count(*) as RiderAvg

  • Thanks, that's what I was looking for!

  • Why do a CONVERT on every row?  CONVERT the SUM instead:

    SELECT

    CONVERT(double, SUM(RiderCount)) / count(*) as RiderAvg

  • Thanks for the optimization. How about using CAST instead of CONVERT, is there preference for one over the other?

    Thanks.

  • I am not aware of any performance difference.  I find CAST more readable, but CONVERT provides an extra Style parameter to control formatting.  Use whichever one makes you happy.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply