pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Katka a Daniel Dunajsky <daniel_katka@hotmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: How to increase precision?
Date: Fri, 2 May 2003 15:32:45 -0700
Message-ID: <200305021532.45697.josh@agliodbs.com> (raw)
In-Reply-To: <Law12-F72YKpDS7hx2700007b3c@hotmail.com>
References: <Law12-F72YKpDS7hx2700007b3c@hotmail.com>

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




view thread (4+ messages)  latest in thread

Message-ID: <200305021532.45697.josh@agliodbs.com>
Permalink:  ../200305021532.45697.josh@agliodbs.com/
Also on:    postgresql.org/message-id/200305021532.45697.josh@agliodbs.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, daniel_katka@hotmail.com
  Subject: Re: How to increase precision?
  In-Reply-To: <200305021532.45697.josh@agliodbs.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox