Re: How to increase precision? - Mailing list pgsql-sql

From Josh Berkus
Subject Re: How to increase precision?
Date
Msg-id 200305021532.45697.josh@agliodbs.com
Whole thread Raw
In response to How to increase precision?  ("Katka a Daniel Dunajsky" <daniel_katka@hotmail.com>)
List 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 2or 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 BerkusAglio Database SolutionsSan Francisco



pgsql-sql by date:

Previous
From: Steve Crawford
Date:
Subject: Re: How to increase precision?
Next
From: "Katka a Daniel Dunajsky"
Date:
Subject: Time formating