Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NULLIF to divide by zero

This is the error I'm receiving below:

Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.

Having looked around at solutions it looks like I need to use NULLIF or ISNULL, I'm just unsure as to how to include it within the line below.

select  
min(round(OnCallTime*100/TotalTime,1)) as total

I'm using SQL Management Studio 2012

like image 781
Clem_Fandango Avatar asked Sep 12 '25 16:09

Clem_Fandango


1 Answers

Use NULLIF in denominator like below:

select  
min(round((OnCallTime*100/NULLIF(TotalTime,0)),1)) as total

So, whenever TotalTime is zero, it'll be replaced by NULL and you will not get the error of Division by Zero.

like image 184
Mayank Porwal Avatar answered Sep 14 '25 06:09

Mayank Porwal