[dba-SQLServer] Div Zero problem

Arthur Fuller fuller.artful at gmail.com
Tue Aug 5 09:57:43 CDT 2008


I'm running into a problem where the sum of one column (call it B) could be
zero and then we want to divide column A's value by Sum(B), and if Sum(B) =
0, then I just want the value of A.

i.e.

a = 480
Sum(b) = 0  -- they are zeroes not nulls
result = a/b but with some sort of divZero function that returns a in the
event that b = zero. IOW if b = zero then b = 1.

I can write a UDF if necessary but my feeling is that this already exists in
MS SQL, I just can't put my finger on it.

Arthur



More information about the dba-SQLServer mailing list