Sql server除以零问题

use*_*705 -5 sql t-sql database sql-server sql-server-2008

请有人帮我解决这个问题:我有这个选择:

SELECT Cast(( Isnull(price, 0) + Isnull(notarycosts, 0)
              + Isnull(landtax, 0) + Isnull(othertaxes, 0)
              + Isnull(agentfee, 0) + Isnull(cadastralfee, 0)
              + Isnull(tabulationfee, 0)
              + Isnull(certsarcini, 0) ) / ( totalareasqm / 10000 * fixhist ) AS
                   DECIMAL(12, 4)) AS EurPerHa
Run Code Online (Sandbox Code Playgroud)

有时我得到除零错误和我的应用程序它被阻止,直到我删除数据库的最后一行.我能以某种方式解决这个问题吗?

谢谢!

bil*_*nkc 5

要么是0 TotalAreaSqm还是FixHist0.因为我们不能除以零,当零存在时你的脚本应该做什么?

我通常会看到将其设为NULL或使用已知值使其工作的方法.我不知道什么是安全的价值.

使其为null方法

select cast((isnull(price,0)+isnull(notarycosts,0)+isnull(landtax,0)+isnull(othertaxes,0)+isnull(agentfee,0)+isnull(cadastralfee,0)+isnull(tabulationfee,0)+isnull(certsarcini,0))/(NULLIF(TotalAreaSqm, 0)/10000*NULLIF(FixHist, 0) as decimal(12,4)) as EurPerHa
Run Code Online (Sandbox Code Playgroud)