PostgreSQL error code
PostgreSQL error 2201E
2201E is the SQLSTATE PostgreSQL reports for invalid argument for logarithm, in class 22, Data Exception.
| SQLSTATE | 2201E |
|---|---|
| Condition name | invalid_argument_for_logarithm |
| Class | 22 — Data Exception |
Reproduce it, then fix it
The logarithm of zero or a negative number is undefined. nullif turns the zero into NULL, so that row gets a NULL instead of stopping the query. Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.
SELECT x, ln(x) FROM (VALUES (1), (10), (0)) AS v(x);
SELECT x, ln(nullif(x, 0)) FROM (VALUES (1), (10), (0)) AS v(x);