Re: Oracle Decode Function

From: Marc Lavergne <mlavergne-pub(at)richlava(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Oracle Decode Function
Date: 2002-07-25 14:01:31
Message-ID: 3D4004BB.4010402@richlava.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> I would like to implement a function similar to the Decode function in
> Oracle.

Take a look at the CASE WHEN ... THEN functionality. For example:

Oracle:
select decode(col1,'abc',1,'xyz',2,0) from test;

Postgresql:
select case when col1 = 'abc' then 1 when col1 = 'xyz' then 2 else 0 end
from test;

> I was wondering if it is possible to accept a variable number
> of parameters (array??).

If you're asking about whether a custom function can have vararg
parameters, the answer appears to depend on the CREATE FUNCTION syntax.
I've never used them personally, but the PG_FUNCTION_ARGS and
PG_GETARG_xxx(#) macros (/src/includes/fmgr.h) available for compiled
functions would appear to support variable length argument lists. The
problem is that I couldn't pin down a CREATE FUNCTION that provided the
same vararg functionality. Hopefully somebody can answer this conclusively.

If it can't be done using custom functions, it should be implementable
"internally" using the same concepts used to support the IN() function
so maybe take a look in /src/backend/parser/parse_func.c for a start.

Edwin S. Ramirez wrote:
> Hello,
>
> I would like to implement a function similar to the Decode function in
> Oracle. I was wondering if it is possible to accept a variable number
> of parameters (array??).
>
> Thanks,
> Edwin S. Ramirez
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2002-07-25 14:50:26 Re: bug in COPY
Previous Message John Liu 2002-07-25 13:55:53 why?