SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


divided by zero error


divided by zero error

Author
Message
jess.
jess.
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 41
Hello,
I am reviving this error when trying to run a query

(48 row(s) affected)
Dec 25 2011 3:30AM
Jan 1 2012 3:30AM

(5 row(s) affected)
Msg 8134, Level 16, State 1, Line 64
Divide by zero error encountered.

This is the code giving me trouble:

select
case
when r1.su is null then r2.su
when r1.su != r2.su then r1.su+'~'+r2.su
else r1.su
end as su,
case
when r1.cur is null then r2.cur
when r1.cur != r2.cur then r1.cur+'~'+r2.cur
else r1.cur
end as cur,r1.dname,r1.rate,r2.dname,r2.rate,round(((r1.rate-r2.rate)/(r1.rate+r2.rate))*100.0,2) as perc

this is how I tried to fix it:

select
case
when r1.su is null then r2.su
when r1.su != r2.su then r1.su+'~'+r2.su
else r1.su
end as su,
case
when r1.cur is null then r2.cur
when r1.cur != r2.cur then r1.cur+'~'+r2.cur
else r1.cur
end as cur,r1.dname,r1.rate,r2.dname,r2.rate,
case
when (r1.rate+r2.rate) = 0 then 0
else round(((r1.rate-r2.rate)/(r1.rate+r2.rate))*100.0,2)
end as perc

Any advice as to what I may be doing wrong?

thanks,
.jess
Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)

Group: General Forum Members
Points: 6589 Visits: 25602
Try changing

case 
when (r1.rate+r2.rate) = 0 then 0
else round(((r1.rate-r2.rate)/(r1.rate+r2.rate))*100.0,2)
end as perc



to

isnull(round(((r1.rate-r2.rate)/(nullif(r1.rate+r2.rate,0)))*100.0,2),0)  as perc



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




jess.
jess.
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 41
I'm still getting the same error when using that.
Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)

Group: General Forum Members
Points: 6589 Visits: 25602
Can you post some DDL and sample data?

____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




GSquared
GSquared
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59185 Visits: 9730
What datatypes are the rate columns? Float?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
jess.
jess.
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 41
This has been fixed. Basically, there were a few other divisors in the query that I didn't check as I thought it had something to do with that section alone.
I added case statements to the other divisors and it worked.

Thanks for your help!
jess.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search