Skip to content
Advertisement

Decode equivalent in postgres

There is no equivalent to the Oracle‘s DECODE()'Function InPostgres`. Is there anyone who wrote decode as a Function?

Advertisement

Answer

There is an equivalent. It’s called a CASE statement.

There are two forms of CASE:

Simple CASE:

CASE search-expression
    WHEN expression [, expression [ ... ]] THEN
      statements
  [ WHEN expression [, expression [ ... ]] THEN
      statements
    ... ]
  [ ELSE
      statements ]
END CASE;

Searched CASE:

CASE
    WHEN boolean-expression THEN
      statements
  [ WHEN boolean-expression THEN
      statements
    ... ]
  [ ELSE
      statements ]
END CASE;

CASE statements are easier to read; I prefer these over decode() in Oracle.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement