Obtain a result with decimals in SQL Server

1

I am dividing two numbers in excel as follows:

352/440 and the result is: 0.80

I need to do the same in SQL Server, this is my query:

SELECT CONVERT(FLOAT,352 / 440)

But the result is 0.

    
asked by ARR 11.09.2018 в 16:49
source

2 answers

2

The result is 0 because you are doing the conversion after doing the division. When using a division with two integers, SQL makes an "integer division", where 352/440 is in effect 0 (the integer part of 0.8).

To avoid this there are several ways, but in general you have to convert (explicitly or implicitly) either the numerator or denominator to decimal or float.

For example with explicit conversion:

SELECT CONVERT(FLOAT,352)/440;

Or implied:

SELECT 352*1.0/440;
    
answered by 11.09.2018 / 16:53
source
2

It is solved by first converting the Numbers to decimal and then dividing

SELECT CONVERT(decimal(12,2),352)  /  CONVERT(decimal(12,2),440)
    
answered by 11.09.2018 в 16:56