Sql server被零除问题
请有人帮我解决这个问题: 我有以下选择:Sql server被零除问题,sql,sql-server,database,sql-server-2008,tsql,Sql,Sql Server,Database,Sql Server 2008,Tsql,请有人帮我解决这个问题: 我有以下选择: SELECT Cast(( Isnull(price, 0) + Isnull(notarycosts, 0) + Isnull(landtax, 0) + Isnull(othertaxes, 0) + Isnull(agentfee, 0) + Isnull(cadastralfee, 0) + Isnull(tabulationfee, 0)
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
有时我会收到一个被零除的错误,我的应用程序会被阻止,直到我从数据库中删除最后一行。
我能解决这个问题吗
谢谢 检查不为空且不为0的
FixHist
和TotalAreaSqm
代码:
TotalAreaSqm
或FixHist
为0。由于我们不能被零除,当零存在时,脚本应该做什么
我通常看到的方法要么使其为NULL,要么使用已知值使其工作。我不知道安全值是多少
使其为空方法
被零除的可能重复项来自totalareasqm或fixhist=0,因此请在查询中添加一个检查该错误的位置。在将错误作为问题发布到此处之前,我会尝试在谷歌上对其进行简单搜索。被零除)返回一组线程,这些线程可能会在比它短得多的时间内回答问题在这里投递;-)它处理空值,而不是当除数为零时…是的,完全忘记了它,感谢sincomplete,TotalAreaSqm也可以为零
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
( isnull(totalareasqm,1) / 10000 * isnull(fixhist,1) )
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
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