Using T-SQL how do you calculate a test grade percentage?
-
26-09-2019 - |
Question
It what should be a simple task, or a simple google search, I have come up empty. I am trying to simply calculate a grade percentage. I am trying to use:
Select round((133 / 150), 2) * 100
That results in 0. I am trying to calcualte to get a score of 89%. I have tried multiple combinations and I am beginning to think it is either too complicated to do with t-sql or it is not possible. What am I missing?
Solution
Try this:
Select round((cast(133 as numeric(20,8))/ cast(150 as numeric(20,8))) * 100, 2)
You are using integers in your example, when you need decimal values. See here for more information.
OTHER TIPS
Try:
Select round(133.0 / 150) * 100
An int
divided by an int
is an int
, and 1 / 2 = 0 (integer truncation)
More generally, if you ensure numerator is a float (real/decimal):
Select round(CAST(integervalue as real) / 150) * 100
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow