pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to increase precision?
4+ messages / 4 participants
[nested] [flat]

* How to increase precision?
@ 2003-05-02 21:38  Katka a Daniel Dunajsky <daniel_katka@hotmail.com>
  0 siblings, 3 replies; 4+ messages in thread

From: Katka a Daniel Dunajsky @ 2003-05-02 21:38 UTC (permalink / raw)
  To: pgsql-sql

Hello all,

this is a newbie question:

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?

Thank you for your time.

Daniel






_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*   
http://join.msn.com/?page=features/junkmail




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to increase precision?
@ 2003-05-02 22:13  Steve Crawford <scrawford@pinpointresearch.com>
  parent: Katka a Daniel Dunajsky <daniel_katka@hotmail.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Steve Crawford @ 2003-05-02 22:13 UTC (permalink / raw)
  To: Katka a Daniel Dunajsky <daniel_katka@hotmail.com>; pgsql-sql

It's assuming INTs and doing int division. Make one a numeric/real or 
explicitly cast:

steve=# select 5/2;
 ?column? 
----------
        2
(1 row)

steve=# select 5./2;
 ?column? 
----------
      2.5
(1 row)

Cheers,
Steve



On Friday 02 May 2003 2:38 pm, Katka a Daniel Dunajsky wrote:
> Hello all,
>
> this is a newbie question:
>
> 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?
>
> Thank you for your time.
>
> Daniel
>
>
>
>
>
>
> _________________________________________________________________
> Help STOP SPAM with the new MSN 8 and get 2 months FREE*
> http://join.msn.com/?page=features/junkmail
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to increase precision?
@ 2003-05-02 22:32  Josh Berkus <josh@agliodbs.com>
  parent: Katka a Daniel Dunajsky <daniel_katka@hotmail.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Josh Berkus @ 2003-05-02 22:32 UTC (permalink / raw)
  To: Katka a Daniel Dunajsky <daniel_katka@hotmail.com>; 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




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to increase precision?
@ 2003-05-02 22:42  Stephan Szabo <sszabo@megazone23.bigpanda.com>
  parent: Katka a Daniel Dunajsky <daniel_katka@hotmail.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Stephan Szabo @ 2003-05-02 22:42 UTC (permalink / raw)
  To: Katka a Daniel Dunajsky <daniel_katka@hotmail.com>; +Cc: pgsql-sql

On Fri, 2 May 2003, Katka a Daniel Dunajsky wrote:

> this is a newbie question:
>
> 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?

It's doing integer division since both arguments are integers. You'll need
to make one of them something like float, double or numeric.

For example, queries like:

select 5./2;
select 5/cast(2 as float);

etc... don't do integer division because one of
the arguments isn't typed as an integer type.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2003-05-02 22:42 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-05-02 21:38 How to increase precision? Katka a Daniel Dunajsky <daniel_katka@hotmail.com>
2003-05-02 22:13 ` Steve Crawford <scrawford@pinpointresearch.com>
2003-05-02 22:32 ` Josh Berkus <josh@agliodbs.com>
2003-05-02 22:42 ` Stephan Szabo <sszabo@megazone23.bigpanda.com>

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