Saturday 19 January 2013

Calculate percentages with decimals in SQL?

Working with SQL Server recently I needed to calculate a percentage using values in other fields.  I went with the obvious field1 / field2 * 100 but got only a result of zero ?

After talking to everyone’s friend Google I came across the following stackover question and answer. It had the following answer:

CONVERT(
    DECIMAL(5,2),
      ( 100 * CONVERT(DECIMAL(5,2),[field1]) / CONVERT(DECIMAL(5,2),[field2]) )
) AS MyPercentAnswer

I hope it proves useful to you.

With thanks to those who answered the questions.

No comments:

Post a Comment

Please, no purely anonymous comments, they will be deleted; always use a name for ease of reference by other commenters.