Thread: Simple function

Simple function

From
MaRCeLO PeReiRA
Date:
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.

So I would do:

"SELECT id,function() FROM mytable;"

and get rows like these:

001 A
002 A
003 C
004 C
005 C
006 B
...

Could you help me???

Thanks in advance,

See ya,

Marcelo

__________________________________________________
Converse com seus amigos em tempo real com o Yahoo! Messenger
http://br.download.yahoo.com/messenger/

Re: Simple function

From
Bruno Wolff III
Date:
On Tue, Dec 07, 2004 at 14:49:14 -0300,
  MaRCeLO PeReiRA <gandalf_mp@yahoo.com.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;"