Search code examples
sqlsql-servert-sqlinteger-division

Using T-SQL how do you calculate a test grade percentage?


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.