Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an accurate percentage calculation

Tags:

sql

sql-server

I am trying to divide one number by another in a SQL view. I'm dividing two columns that are each of type int, and the problem is that its giving me a rounded output, rather than an accurate one.

Here is my select:

SELECT numberOne, numberTwo, (numberOne*100/NULLIF(numberTwo, 0)) as PctOutput

This is working, but when it dives 3 by 37 its giving me 8 instead of 8.108.

How would I modify this to give me an accurate answer? Do I need to cast the numbers out of ints? if so - into what?

like image 734
kaes Avatar asked Dec 31 '25 16:12

kaes


1 Answers

Try an implicit cast:

SELECT 
   numberOne, 
   numberTwo, 
   ((1.0*numberOne)*100/NULLIF(1.0*numberTwo, 0)) as PctOutput
like image 121
dani herrera Avatar answered Jan 02 '26 06:01

dani herrera