-5

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

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

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

谢谢!

4

4 回答 4

5

要么 要么TotalAreaSqmFixHist0。由于我们不能除以零,当零存在时你的脚本应该做什么?

我通常会看到将其设为 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
于 2012-12-06T20:40:18.970 回答
0

检查FixHist并且TotalAreaSqm不是空值,也不是 0。

代码:

select CASE WHEN ISNULL(NULLIF(FixHist,0),0) !=0 
                 AND ISNULL(NULLIF(TotalAreaSqm,0),0) !=0
THEN
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))
ELSE 0 END as EurPerHa
于 2012-12-06T20:36:21.460 回答
0
SELECT IIF( TotalAreaSqm*FixHist=0
            ,null
            , 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
于 2012-12-06T20:44:37.373 回答
-3
( isnull(totalareasqm,1) / 10000 * isnull(fixhist,1) )
于 2012-12-06T20:38:54.617 回答