Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Divide by zero - Sum with Case

Author  Topic 

artvindustries
Starting Member

1 Post

Posted - 2013-04-29 : 18:55:46
I want to protect the following statement from a divide by zero error. if v.Loan_LoanAmt is 0 than just return 0. How can I achieve this?


select [MyColumn] =
sum( case when v.Process_FundingDt between @begDt and @endDt and v.Loan_Purpose_Desc = 'PURCHASE' then v.Loan_LoanAmt else 0 end)
/ sum (case when v.Process_FundingDt between @begDt and @endDt then v.Loan_LoanAmt else 0 end)


Thanks for any help!

-Rick-

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-04-29 : 19:31:18
This is a basic pattern to handle that:

SELECT COALESCE(Numerator / NULLIF(Denominator, 0), 0)

You might be able to use just use a COALESCE and repalce "else 0" with "else NULL"
Go to Top of Page
   

- Advertisement -