Re: How to increase precision?

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: "Katka a Daniel Dunajsky" <daniel_katka(at)hotmail(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: How to increase precision?
Date: 2003-05-02 22:32:45
Message-ID: 200305021532.45697.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hey Katka,

> How to increase the precision of calculations in posgresql?
>
> When I run this query:
>
> select 5/2;
>
> I get:
>
> ?column?
> --------
> 2
>
> It should be 2.5 shouldn't it?

Nope! You're thinking like a normal person, not like a database engineer.
Translated, here's what you told the database:

Give me INTEGER 5 / INTEGER 2

and the database told you:

INTEGER 2

if you'd asked, the database would also have happily told you:

REMAINDER 1

What you really wanted to ask the database was:

Give me NUMERIC 5 / NUMERIC 2
or in SQL:
SELECT 5::NUMERIC/2::NUMERIC

At which point you will get

2.5000 (NUMERIC)

Look for a general book on SQL databases, which should be able to give you a
primer on data types and casting.

--
-Josh Berkus
Aglio Database Solutions
San Francisco

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Katka a Daniel Dunajsky 2003-05-02 22:33:56 Time formating
Previous Message Steve Crawford 2003-05-02 22:13:31 Re: How to increase precision?