SQL Server,除法返回零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1666407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:15:16  来源:igfitidea点击:

SQL Server, division returns zero

sql

提问by Roch

Here is the code I'm using in the example:

这是我在示例中使用的代码:

 PRINT @set1
 PRINT @set2

 SET @weight= @set1 / @set2;
 PRINT @weight

Here is the result:

结果如下:

47
638
0

I would like to know why it's returning 0instead of 0,073667712

我想知道为什么它返回0而不是0,073667712

回答by Martin

Either declare set1 and set2 as floats instead of integers or cast them to floats as part of the calculation:

要么将 set1 和 set2 声明为浮点数而不是整数,要么将它们转换为浮点数作为计算的一部分:

SET @weight= CAST(@set1 AS float) / CAST(@set2 AS float);

回答by Hans Ke?ing

When you use only integers in a division, you will get integer division. When you use (at least one) double or float, you will get floating point division (and the answer you want to get).

当你在一个除法中只使用整数时,你会得到整数除法。当您使用(至少一个)double 或 float 时,您将得到浮点除法(以及您想要得到的答案)。

So you can

这样你就可以

  1. declare one or both of the variables as float/double
  2. cast one or both of the variables to float/double.
  1. 将一个或两个变量声明为 float/double
  2. 将一个或两个变量强制转换为 float/double。

Do not just cast the result of the integer division to double: the division was already performed as integer division, so the numbers behind the decimal are already lost.

不要只是将整数除法的结果转换为 double:除法已经作为整数除法执行,因此小数后面的数字已经丢失。

回答by Hans Ke?ing

Because it's an integer. You need to declare them as floating point numbers or decimals, or cast to such in the calculation.

因为它是一个整数。您需要将它们声明为浮点数或小数,或在计算中转换为此类。

回答by HLGEM

Simply mutiply the bottom of the division by 1.0 (or as many decimal places as you want)

只需将除法的底部乘以 1.0(或您想要的小数位数)

PRINT @set1 
PRINT @set2 
SET @weight= @set1 / @set2 *1.00000; 
PRINT @weight

回答by anishMarokey

if you declare it as float or any decimal format it will display

如果您将其声明为浮点数或任何十进制格式,它将显示

0

0

only

只要

E.g :

例如:

declare @weight float;

SET @weight= 47 / 638; PRINT @weight

Output : 0

输出:0

If you want the output as

如果您希望输出为

0.073667712

0.073667712

E.g

例如

declare @weight float;

SET @weight= 47.000000000 / 638.000000000; PRINT @weight

回答by Suat Atan PhD

In SQL Server direct division of two integer returns integer even if the result should be the float. There is an example below to get it across:

在 SQL Server 中,两个整数的直接除法返回整数,即使结果应该是浮点数。下面有一个例子来理解它:

--1--
declare @weird_number_float float
set @weird_number_float=22/7
select @weird_number_float

--2--
declare @weird_number_decimal decimal(18,10)
set @weird_number_decimal=22/7 
select @weird_number_decimal

--3--
declare @weird_number_numeric numeric
set @weird_number_numeric=22/7 
select @weird_number_numeric

--Right way

declare @weird_number float
set @weird_number=cast(22 as float)/cast(7 as float)
select @weird_number

Just last block will return the 3,14285714285714. In spite of the second block defined with right precision the result will be 3.00000.

就在最后一个块将返回 3,14285714285714。尽管以正确的精度定义了第二个块,但结果将为 3.00000。