Redshift division result does not include decimals

Andres Urrego Angel picture Andres Urrego Angel · May 23, 2018 · Viewed 14.9k times · Source

I'm trying to do something really quite basic to calculate a kind of percentage between two columns in Redshift. However, when I run the query with an example the result is simply zero because the decimals are not being covered.

code:

select 1701 / 84936;

Output:

enter image description here

I tried :

select cast(1701 / 84936 as numeric (10,10));

but the result was 0.0000000000.

How could I solve this silly thing?

Answer

Lukasz Szozda picture Lukasz Szozda · May 23, 2018

It is integer division. Make sure that at least one argument is: NUMERIC(accurate data type)/FLOAT(caution: it's approximate data type):

/ division (integer division truncates the result)

select 1701.0 / 84936;
-- or
SELECT 1.0 * 1701 / 84936;
-- or
SELECT CAST(1701 AS NUMERIC(10,4))/84936;

DBFiddle Demo