Question : Divide By Zero in Query Calculation

The query below generates a divide by zero error. I've tried adding a IsNull statement so that I can catch the negative number and know that the sum of the Costs was zero but I can't get it working.

Any ideas?

      SELECT tblProject.ExpID, tblProject.ProjID, tblProject.ProjectTitle, tblProject.Date, tblProject.Originator, tblProject.Approved, tblProject.Sent
      , Savings1+Savings2+Savings3+Savings4+Savings5 AS TotSave
      , (SELECT Sum(tblEstCost.Cost)*1.1 FROM tblEstCost WHERE ExpID=tblProject.ExpID) AS TotCost
      , CAST(((Savings1+Savings2+Savings3+Savings4+Savings5)/IsNull((SELECT Sum(tblEstCost.Cost)*1.1 FROM tblEstCost WHERE ExpID=tblProject.ExpID), -1))*100 AS money) AS IRR
       FROM tblProject
       WHERE (((tblProject.Approved)<>1 And (tblProject.Approved)<>5 And (tblProject.Approved)<>2) AND ((tblProject.Sent)='SENT'))  and (ToCapex <> 'Y' or ToCapex Is Null)
       and (Date1 >= '" & CleanDate(session("rptDateFrom")) & "' and Date1 <= '" & CleanDate(session("rptDateTo")) & "')"
       Order By Date

Answer : Divide By Zero in Query Calculation

Replace

CAST(((Savings1+Savings2+Savings3+Savings4+Savings5)/IsNull((SELECT Sum(tblEstCost.Cost)*1.1 FROM tblEstCost WHERE ExpID=tblProject.ExpID), -1))*100 AS money)

to

CAST(((Savings1+Savings2+Savings3+Savings4+Savings5)/IsNull(NullIf(SELECT Sum(tblEstCost.Cost)*1.1 FROM tblEstCost WHERE ExpID=tblProject.ExpID), 0), -1))*100 AS money)
Random Solutions  
 
programming4us programming4us