Divide in SQL Server

假装没事ソ 提交于 2019-12-07 11:09:02

问题


In SQL Server 2005 Express, the result is below

SELECT 100 / 15 --Result 6

But I wanna get approximate value 7 (like using calculator)

100 / 15 = 6.6666..

How to make it in SQL Server?


回答1:


You have to use decimal numbers e.g. 100 / 15.0. If you use integers the result is interpreted as an integer and truncated to the largest integer that is smaller than or equal to the result.




回答2:


SELECT cast(100 as float) / (15 as float)




回答3:


You want SQL Server to perform floating-point divison, as opposed to integer division. If you're using literal values, as in your example, suffix the denominator with .0 to force SQL Server to treat it as a floating-point value.

SELECT 100 / 15.0

Otherwise, make sure you declare your variables as FLOAT.




回答4:


Try declaring variables as floats:

DECLARE @var1 FLOAT
DECLARE @var2 FLOAT
SET @var1 = 100
SET @var2 = 15
SELECT @var1 / @var2


来源:https://stackoverflow.com/questions/5151018/divide-in-sql-server

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