Error
Error Code:
22012
PostgreSQL Error 22012: Division by Zero
Description
This error indicates an attempt to perform a mathematical division operation where the divisor (denominator) evaluates to zero. It's a standard SQL data exception that prevents the query or transaction from completing successfully, ensuring data integrity.
Error Message
division by zero
Known Causes
3 known causesDirect Zero Denominator in Query
Occurs when an SQL query directly attempts to divide by the literal value zero, for example, `SELECT 10 / 0;`.
Column or Variable Evaluates to Zero
A common cause is when a column or variable used as a divisor in an expression contains or evaluates to a zero value for one or more rows.
Aggregate Function Miscalculation
Can happen in aggregate functions or complex expressions where the denominator part of a calculation results in zero due to filtering or specific data conditions.
Solutions
4 solutions available1. Handle Zero Denominators with CASE easy
Use a CASE statement to check for zero denominators before performing division.
1
Identify the SQL query or function that is causing the division by zero error. This typically involves a `numerator / denominator` expression.
2
Modify the query to include a `CASE` statement. The `CASE` statement will check if the denominator is zero. If it is, you can return a default value (e.g., 0, NULL) or handle it as appropriate for your business logic. Otherwise, perform the division.
SELECT numerator / CASE WHEN denominator = 0 THEN NULL ELSE denominator END AS result FROM your_table;
3
Alternatively, you can return 0 if the denominator is zero, which might be more suitable in some aggregation scenarios.
SELECT numerator / CASE WHEN denominator = 0 THEN 0 ELSE denominator END AS result FROM your_table;
2. Filter Out Zero Denominators with WHERE Clause easy
Exclude rows where the denominator is zero from the division operation.
1
Locate the SQL query where the division by zero is occurring.
2
Add a `WHERE` clause to your query to filter out rows where the denominator column has a value of 0.
SELECT numerator / denominator AS result FROM your_table WHERE denominator <> 0;
3
If you need to include rows where the denominator is zero but want to handle them differently (e.g., assign a specific value), you might combine this with a `CASE` statement or use a subquery.
SELECT numerator / denominator AS result FROM your_table WHERE denominator <> 0 UNION ALL SELECT 0 AS result FROM your_table WHERE denominator = 0;
3. Use NULLIF for Zero Denominators easy
Employ the NULLIF function to convert zero denominators to NULL, preventing division by zero.
1
Find the SQL statement that performs the division.
2
Wrap the denominator in the `NULLIF()` function. `NULLIF(expression1, expression2)` returns NULL if `expression1` equals `expression2`, otherwise it returns `expression1`. In this case, if the denominator is 0, it will become NULL.
SELECT numerator / NULLIF(denominator, 0) AS result FROM your_table;
3
Note that division by NULL in PostgreSQL results in NULL. If you want to substitute a different value for NULL results (e.g., 0), you can use the `COALESCE` function.
SELECT COALESCE(numerator / NULLIF(denominator, 0), 0) AS result FROM your_table;
4. Data Validation and Cleansing medium
Proactively prevent zero values in denominator columns through data constraints or ETL processes.
1
Analyze the data model and identify columns that are used as denominators in critical calculations.
2
Implement a `CHECK` constraint on the table to disallow zero values in the denominator column.
ALTER TABLE your_table ADD CONSTRAINT non_zero_denominator CHECK (denominator <> 0);
3
If direct constraint addition is not feasible or you need to clean existing data, develop an ETL (Extract, Transform, Load) process or a script to identify and correct or handle existing zero values in the denominator column. This might involve updating the values, archiving problematic records, or flagging them for review.
UPDATE your_table SET denominator = NULL WHERE denominator = 0; -- Or another appropriate value
4
Consider using triggers to enforce the non-zero constraint on `INSERT` and `UPDATE` operations if a `CHECK` constraint is not sufficient or if you need more complex logic.
-- Example trigger function (simplified)
CREATE OR REPLACE FUNCTION prevent_zero_denominator() RETURNS TRIGGER AS $$
BEGIN
IF NEW.denominator = 0 THEN
RAISE EXCEPTION 'Denominator cannot be zero.';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER check_denominator_trigger
BEFORE INSERT OR UPDATE ON your_table
FOR EACH ROW EXECUTE FUNCTION prevent_zero_denominator();