Here is an easy way to use NULLIF function to handle division by zero error.
NULLIF - Returns a null value if the two specified expressions are equal.
For example:
DECLARE @a INT
DECLARE @div INT
SET @a = 10
SET @div = 0
SELECT ISNULL(@a / NULLIF(@div,0),@a) -- Returns @a when @div = 0
SELECT ISNULL(@a / NULLIF(@div,0),0) -- Returns 0 when @div = 0 |