Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Force show decimal values

Tags:

sql

firebird

I'm using a Firebird database and am trying the following sql but each time it returns 0, instead of 0.61538 (etc).

SELECT (COUNT(myfield)/26) totalcount
FROM mytable

Now when I remove the /26, the totalcount returns 16 as it should. But when I add the divided by 26 back in, the result shows as 0, but it should show as the full decimal value of 0.615384... Does anyone know why it's not returning the full value? I've even tried wrapping it in a CAST((count(myfield)/26) as double) totalcount but it still returns 0.

Thank you in advance for any suggestions!!!!

like image 405
Phil Avatar asked Feb 26 '26 14:02

Phil


1 Answers

Try:

SELECT COUNT(myfield/26.0) totalcount
FROM mytable

Or:

SELECT COUNT(CAST(myfield as double)/26) totalcount
FROM mytable

Not familiar with Firebird, but in other implementations you have to cast/convert either the numerator or denominator as a decimal before division, as integer division returns an integer value.

like image 60
Hart CO Avatar answered Mar 01 '26 02:03

Hart CO