I am trying to run a script in SQL in which I am dividing but I am getting the error "Divide by zero error encountered"
I tried to add in NULLIF which was suggested in an old post I read but this didnt help it.
SELECT SUM([SE Gross]) AS [Donors Gross],
ROUND((SUM([Cancelled]) * 100) / NULLIF(SUM([SE Gross]), 2), 0),
SUM([Venue Cost]),
SUM([Accom/Expenses]),
ROUND(SUM([SE Gross]) / NULLIF(SUM([fundraising days]), 2), 0) AS Column1,
ROUND(SUM([Venue Cost]) / NULLIF(SUM([SE Gross]), 2), 0) AS Column2,
ROUND(SUM([Venue Cost] + [Accom/Expenses]) / NULLIF(SUM([SE Gross]), 2), 0) AS column3,
ROUND(SUM([Venue Cost] + [Accom/Expenses]) / NULLIF(SUM([donor net COUNT]), 2), 0) AS column4
FROM dbo.TEST1;
The result I currently get is: Divide by zero error encountered.
ROUNDandNULLIF. You're currently usingNULLIF(SUM([SE Gross]), 2), which will turn the result intoNULLifSUM([SE Gross])is exactly equal to 2. You wantNULLIF(SUM([SE Gross]), 0)(and round to 2 digits, presumably). - Jeroen Mostertisnull. Do you understand whatnullifis supposed to achieve here? There's no need to admonish op quite so aggressively (especially when you apparently don't understand what they are trying to achieve). - HoneyBadger