2012-12-06 107 views
0

可能重複:
How to avoid the 「divide by zero」 error in SQL?如何避免被零錯誤劃分

我有一個關於Sql Server的零錯誤劃分的問題。 有時候這個錯誤和它被阻塞的視圖直到我刪除相應的行。 你能幫我,給我一些建議,我該如何避免這種情況? 謝謝。

CREATE VIEW Acq 
AS 
SELECT 
     ac_id 
     ,[Company] 
     ,No 
     ,[ContractID] 
     ,[Seller] 
     ,[AcquistionDate] 
     ,[Village] 
     ,[Commune] 
     ,[Area] 
     ,[PlotArea] 
     ,[FieldNo] 
     ,[Topo1] 
     ,[Topo2] 
     ,[Topo3] 
     ,[Topo4] 
     ,[Topo5] 
     ,[TotalAreaSqm] 
     ,[OwnershipTitle] 
     ,[CadastralNO] 
     ,[Type] 
     ,[Price] 
     ,[NotaryCosts] 
     ,[LandTax] 
     ,[OtherTaxes] 
     ,[AgentFee] 
     ,[CadastralFee] 
     ,[TabulationFee] 
     ,[CertSarcini] 
     ,[ProcuraNO] 
     ,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)) as decimal(12,4)) as TotalCosts 
     ,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) as decimal(12,4)) as RonPerHa 
     ,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 
     ,[DeclImpunere] 
     ,[FixHist] 
     ,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))/FixHist as decimal(12,4)) as EurHist 
     ,[LandStatus] 


FROM  tblAcq 

回答

3

(nullif(TotalAreaSqm,0)/10000)

更換喜歡(TotalAreaSqm/10000)表情這裏假設你想結果被零是在分割的情況下null。有關nullif的說明,請參閱文檔here

+0

非常感謝,它的工作! – user1820705

1
CASE (TotalAreaSQM/1000 <> 0) THEN 'do work' ELSE 'dont do it and pass the 0 or what ever' 

的,你可以寫simmilar東西Gazilion。

+0

謝謝你的幫忙! – user1820705