0

Possible Duplicate:
How to avoid the “divide by zero” error in SQL?

I have an issue regarding Sql Server divided by zero error. Sometimes this error and the view it's blocked until I delete the respective row. Can you please help me and give me some advices in how could I avoid this? Thanks.

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
Community
  • 1
  • 1
user1820705
  • 591
  • 2
  • 7
  • 17

2 Answers2

4

Replace expressions like (TotalAreaSqm/10000) with (nullif(TotalAreaSqm,0)/10000)

This assumes you would like the result to be null in the case of division by zero. See the docs here for a description of nullif.

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

Gazilion of simmilar stuff you can write.

Gustav Klimt
  • 430
  • 3
  • 14