Re: simple division

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: simple division
Date: 2018-12-04 21:38:48
Message-ID: 1cbf43df-b495-f5f2-fcfd-b610a279e17b@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Martin Mueller schrieb am 04.12.2018 um 21:57:
> I didn't formulate my question properly, because the query went like
> "select alldefects /wordcount"
> where alldefects and wordcount are integers.
> But none of the different ways of putting the double colon seemed to
> work.
One way is to make one of the integers a decimal by multiplying with 1.0

select alldefects * 1.0 / wordcount

> The Postgres notation of this simple procedure is very unintuitive. I
> haven't been able to remember several times, and most people think of
> me as a person with a reasonably good memory.

Postgres supports the SQL standard's CAST operator:

select cast(alldefects as decimal) / wordcount

The "Postgres way" would be:

select alldefects::decimal / wordcount

> There is no obvious place in the documentation to look this up.

This is covered in the chapter "Type Casts"

https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2018-12-04 21:51:31 Re: simple division
Previous Message Martin Mueller 2018-12-04 21:36:35 Re: simple division