Decode equivalent in postgres

前端 未结 3 775
一整个雨季
一整个雨季 2021-02-13 02:36

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

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 03:10

    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.

提交回复
热议问题