除以零 - 无法理解为什么会失败

时间:2017-05-09 12:36:13

标签: sql sql-server tsql

我已经编写了一个函数,只是为了解决Pro-Rata Clawback的任何数量我不断打字。

然而,每次运行时我都会得到Divide By Zero Error,我非常简单地打破了这个功能。

DECLARE @ProRataAmount DECIMAL(8,2), @DaysRemaining INT,@NetValue DECIMAL(8,2), @TimeOnRisk INTEGER

SET @NetValue = 29.00
SET @TimeOnRisk = 8

SET @DaysRemaining = (365 - @TimeOnRisk)

PRINT @DaysRemaining
PRINT @NetValue

SELECT @NetValue / (@DaysRemaining/365)

那里没有除以零的值,为什么它会返回错误?

干杯。

2 个答案:

答案 0 :(得分:5)

INT / INT将返回INT。

请尝试以下操作(请注意365.0):

SELECT @NetValue / (@DaysRemaining/365.0)

现在,为了防止@DaysRemaining为ZERO,你仍然可以产生一个除以零,除非你像这样陷入它:

SELECT @NetValue / (NullIf(@DaysRemaining,0)/365.0)

答案 1 :(得分:-1)

将整数除以整数将导致此处出现问题:

SELECT @NetValue * 365 / @DaysRemaining

这里你总是除以零,因为previous_line="" # start at first line i=1 # suppress first column, that don't need to compare sed 's@^[0-9][0-9]*,@@' ./data_file > ./transform_data_file # for all line within file without first column for current_line in $(cat ./transform_data_file) do # if previous record line are same than current line if [ "x$prev_line" == "x$current_line" ] then # record line number to supress after echo $i >> ./line_to_be_suppress fi # record current line as previous line prev_line=$current_line # increment current number line i=$(( i + 1 )) done # suppress lines for line_to_suppress in $(tac ./line_to_be_suppress) ; do sed -i $line_to_suppress'd' ./data_file ; done rm line_to_be_suppress rm transform_data_file 小于除数。您可以更改计算以轻松过去,同时将值保持为整数:

{{1}}