Division of 2 numbers using CAST function in SQL server 2008R2

女生的网名这么多〃 提交于 2019-12-11 03:05:33

问题


I have two numbers I want to divide: 5262167 / 162333331 When verified using windows calculator (calc.exe) the result is 0.0324158136076195 but when used simple select with CAST function in SQL Server 2008R2 I don't have same result. Here is what I'm running in SQL editor:

select CAST((5262167 / 162333331) as decimal(18,8))

and the result is 0.00000000


回答1:


You're doing integer division, which will truncate any remainders. 5262167 < 162333331, so your result is 0. Cast your input before dividing.

select CAST(5262167 as decimal(18,8)) / CAST(162333331 as decimal(18,8))



回答2:


Another less elegant way is: select ( 5262167 * 1.0 ) / ( 1623333331 * 1.0 ) ). I usually forget to cast them and just multiply by 1.0.



来源:https://stackoverflow.com/questions/16484434/division-of-2-numbers-using-cast-function-in-sql-server-2008r2

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