Division by zero - Mailing list pgsql-general

From Oliver Kohll - Mailing Lists
Subject Division by zero
Date
Msg-id 367A1AAB-F401-4DE5-A21E-4BA68FCF7DE4@gtwm.co.uk
Whole thread Raw
Responses Re: Division by zero
List pgsql-general
Hello,

We have a system that allows users to create views containing
calculations but divisions by zero are commonly a problem.

An simple example calculation in SQL would be

SELECT cost / pack_size AS unit_cost from products;

Either variable could be null or zero.

I don't think there's a way of returning null or infinity for
divisions by zero, rather than causing an error but I'd just like to
check - and put in a vote for that functionality!

If not, I will have to get the system to check for any numeric fields
in user input calculations and rewrite them similar to

CASE WHEN cost IS NULL THEN null
WHEN pack_size IS NULL THEN null
WHEN cost = 0 THEN null
WHEN pack_size = 0 THEN null
ELSE cost / pack_size
AS unit_cost

I don't want to write new functions, I'd rather keep it in plain SQL.

Best regards

Oliver Kohll


oliver@gtwm.co.uk / 0845 456 1810 / 07814 828608
www.gtwm.co.uk - company
www.gtportalbase.com - product

pgsql-general by date:

Previous
From: "Louis Lam"
Date:
Subject: Re: catalog view use to find DATABASE, LANGUAGE, TABLESPACE, SCHEMA, SEQUENCE privileges granted to user or role
Next
From: Erik Jones
Date:
Subject: Re: warm standby with WAL shipping