Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

divided by zero error Expand / Collapse
Author
Message
Posted Wednesday, January 2, 2013 7:27 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 29, 2013 8:17 AM
Points: 14, 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
Post #1401832
Posted Wednesday, January 2, 2013 7:34 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:54 AM
Points: 1,681, Visits: 19,597
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




____________________________________________________

How to get the best help on a forum

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

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1401836
Posted Wednesday, January 2, 2013 7:45 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 29, 2013 8:17 AM
Points: 14, Visits: 41
I'm still getting the same error when using that.
Post #1401849
Posted Wednesday, January 2, 2013 7:49 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:54 AM
Points: 1,681, Visits: 19,597
Can you post some DDL and sample data?

____________________________________________________

How to get the best help on a forum

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

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1401852
Posted Wednesday, January 2, 2013 7:54 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, December 5, 2014 10:31 AM
Points: 13,872, Visits: 9,599
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
Post #1401854
Posted Wednesday, January 2, 2013 8:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 29, 2013 8:17 AM
Points: 14, 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.
Post #1401881
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse