There is few ways at least to avoid 'divide by zero' error.
1)
NULLIF[
^] function - zero is replaced with
NULL
. Any operation on
NULL
returns
NULL
always ;)
SELECT Field1, Field2, Field1/NULLIF(Field2,0) AS Result
FROM TableName
2)
CASE ... END[
^] statement - returns divide result only for non-zero values
SELECT Field1, Field2, CASE WHEN Field2 = 0 THEN 0 ELSE Field1/Field2 END AS Result
FROM TableName
3)
WHERE[
^] clause - returns non-zero values in subquery; note: Field2 data equal to zero are ignored!
SELECT Field1, Field2, Field1/Field2 AS Result
FROM (
SELECT Field1, Field2
FROM TableName
WHERE Field2>0
) AS A