Re: Divide by zero... - Mailing list pgsql-general

From Robert Creager
Subject Re: Divide by zero...
Date
Msg-id 20030930080625.7b890f46.Robert_Creager@LogicalChaos.org
Whole thread Raw
In response to Divide by zero...  (Mike Leahy <mgleahy@fes.uwaterloo.ca>)
Responses Re: Divide by zero...  (Jan Wieck <JanWieck@Yahoo.com>)
List pgsql-general
When grilled further on (Tue, 30 Sep 2003 09:26:19 -0400),
Mike Leahy <mgleahy@fes.uwaterloo.ca> confessed:

> I have a query that calculates various using variables from a survey
> database.  As with any survey, there are many instantces of null values.  I'm
> wondering if there is any way to escape the error caused by dividing by zero
> or null values.  The specific message i get is:
>
> ERROR:  floating point exception! The last floating point operation either
> exceeded legal ranges or was a divide by zero
>
> Is there a simple trick that won't make my queries excessively complex?

I believe CASE and COALESCE will solve your problem.  Something like this:

SELECT CASE COALESCE( denom, 0.0 )
          WHEN 0.0 THEN 0.0
          ELSE COALESCE( num, 0.0 ) / denom
       END
FROM some_table;

Cheers,
Rob

--
 07:48:16 up 60 days, 19 min,  4 users,  load average: 2.32, 2.78, 2.97

Attachment

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: pg_hba.conf
Next
From: Tom Lane
Date:
Subject: Re: Functional index performance question