Re: Simple function

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: MaRCeLO PeReiRA <gandalf_mp(at)yahoo(dot)com(dot)br>
Cc: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: Simple function
Date: 2004-12-11 18:07:01
Message-ID: 20041211180701.GA31773@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Dec 07, 2004 at 14:49:14 -0300,
MaRCeLO PeReiRA <gandalf_mp(at)yahoo(dot)com(dot)br> wrote:
> Hi guys,
>
> I have a table with two integer fields (field1 and
> field2)...
>
> I would like to construct a function that perform an
> operation with this two fiels (field1 + fields2), and
> return the string value "A" if the sum is <20, "B" if
> the sum is >=20 or <=50, and finally return "C" if the
> sum is >50.

Why not use a CASE statement?

SELECT id,
CASE
WHEN field1 + field2 < 20 THEN 'A'
WHEN field1 + field2 > 50 THEN 'C'
ELSE 'B'
END
FROM mytable;"

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-12-11 18:11:24 Re: Checking inequality
Previous Message Vitaly Belman 2004-12-11 17:59:55 Re: Checking inequality