Re: How to increase precision?

Поиск
Список
Период
Сортировка
От Josh Berkus
Тема Re: How to increase precision?
Дата
Msg-id 200305021532.45697.josh@agliodbs.com
обсуждение исходный текст
Ответ на How to increase precision?  ("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 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 по дате отправления:

Предыдущее
От: Steve Crawford
Дата:
Сообщение: Re: How to increase precision?
Следующее
От: "Katka a Daniel Dunajsky"
Дата:
Сообщение: Time formating