We help IT Professionals succeed at work.
Get Started

How to Avoid "Divide by Zero" error - T-SQL

Last Modified: 2012-08-26
Hello Experts,

I'm getting a "Divided by zero" error when running a report using the the following select statement:

SELECT CTE1.ChID, CTE1.ChName, CTE1.GroupRank, CTE1.ChRank, CTE1.StartTime TimeSlot, CTE1.FromDate, AVG(CTE1.Rating) Rating,AVG(CTE1.TRP) TRP,AVG((CTE1.TRP/CTE2.TRP) *100) AS Share
FROM CTE1 INNER JOIN CTE2 on CTE1.FromDate = CTE2.FromDate and CTE1.SheetDay = CTE2.SheetDay and CTE1.SlotOrder = CTE2.SlotOrder

Obviuosly, the error is coming from this part:
AVG((CTE1.TRP/CTE2.TRP) *100) AS Share

Therefore, I need to add a function that will ignore the error. Something like isNull() or isZero(), or whatever...

Appreciate your help
Watch Question
IT Director
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE