SQL Force show decimal values

元气小坏坏 提交于 2019-12-23 19:53:39

问题


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!!!!


回答1:


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.



来源:https://stackoverflow.com/questions/18987484/sql-force-show-decimal-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!