Home > Net >  How to change default column name for Conditional Expressions
How to change default column name for Conditional Expressions

Time:12-31

I am referring to this document -> https://www.postgresql.org/docs/current/functions-conditional.html

SELECT a,
       CASE WHEN a=1 THEN 'one'
            WHEN a=2 THEN 'two'
            ELSE 'other'
       END
    FROM test;

 a | case
--- -------
 1 | one
 2 | two
 3 | other

But can find any StackOverflow question or document explaining how I can change the default column name from "case" to something else?

I tried to wrap it to select statement and give the name like -> SELECT (CASE WHEN END) as columnName, but did not work or I am doing somethigwrong.

CodePudding user response:

Just use AS, the same way you alias any other column:

SELECT a,
       CASE WHEN a=1 THEN 'one'
            WHEN a=2 THEN 'two'
            ELSE 'other'
       END AS some_name
    FROM test;
  • Related