Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Division precision

Tags:

I have 2 columns which I need to divide sum(cola)/sum(ColB), but I am not getting the desired results since SQL server seems to truncate values after decimal For eg. I have-

select 281370/1035

is giving 271 using simple division, whereas actual result of division is 271.8550724637681 and I want to display 271.8

I tried

SELECT cast(round(281370/1035,1) as numeric(36,1))

but that results 271.0

like image 628
SilverFish Avatar asked Jan 29 '18 20:01

SilverFish


1 Answers

In SQL Server, you have to cast the integers to decimal and you could use Round to get desired precision.

SELECT cast(Round(CAST(281370 AS decimal) / CAST(1035 AS decimal),1,1) as decimal(10,1))
like image 200
lucky Avatar answered Sep 19 '22 13:09

lucky