From: | "Josh Berkus" <josh(at)agliodbs(dot)com> |
---|---|
To: | David Link <dlink(at)soundscan(dot)com>, "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org> |
Subject: | Re: DECODE |
Date: | 2001-10-31 01:15:26 |
Message-ID: | web-495383@davinci.ethosmedia.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
David,
> How does one do the equivalence of Oracle's DECODE in PostgreSQL?
>
> select
> decode (v.media, 'V', 'VHS', 'L', 'Laser Disk', 'Unknown')
> from
> videos v
> ;
Use a CASE statement:
SELECT (CASE WHEN v.media = 'V' THEN 'VHS'
WHEN v.media = 'L' THEN 'Laser Disk'
ELSE 'Unknown' END) AS media_exp
FROM videos v;
If you wanted, you could write a PL/pgSQL function to simplify this,
using two array parameters to hold the lists.
HOWEVER, it would be far better than both of the above, relationally,
to
create a reference table populated with the appropriate values
(media_code, media_name) and JOIN the reference table. This would
prevent you from having to populate a special function on each and
every
query!
-Josh
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco
From | Date | Subject | |
---|---|---|---|
Next Message | Sharon Cowling | 2001-10-31 02:53:19 | Returning results from function |
Previous Message | David Link | 2001-10-30 22:41:47 | DECODE |