My goal is two divide two integers in Presto 0.212, e. g. 1/2. The naive approach SELECT 1/2
returns 0. Next, I tried SELECT CAST(1/2 AS DOUBLE)
, but this also returns 0. How to divide 1/2 such that 0.5 is returned?
Asked
Active
Viewed 5,187 times
1 Answers
5
I'm not familiar with Presto, but my guess is that in the example you've provided 1/2
is being evaluated as an integer then is being cast as a double. Maybe something along the lines of SELECT CAST(1 AS DOUBLE)/CAST(2 AS DOUBLE)
or you maybe you could just add .0
to the end of your numbers like SELECT 1.0/2.0
. Just a few shots in the dark from me.

glenjaminlatrobe
- 66
- 4
-
Both work, but as I want to divide columns I choose the first one. – Joe Dec 04 '18 at 19:42
-
1In Presto (following SQL standard), `2.0` is not a `double`, it's of `decimal(2,1)` type. This affects results: `10 / 3e0` prints `3.3333333333333335`, while `10 / 3.0` prints `3.3`. Use scientific notation or explicit type construct to get "2 as double": `2e0` or `double '2'`. – Piotr Findeisen Dec 05 '18 at 07:43