Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name an output column created with case in PostgreSQL

Is it possible to name the output column created with case ("switch") in a PostgreSQL SELECT statement? It appears from the documentation that this is not possible. An example usage of what I would like to do is:

SELECT CASE (column) WHEN 1 THEN 'One' END AS 'TheColumn' FROM TABLE ;

like image 868
SabreWolfy Avatar asked Feb 20 '23 05:02

SabreWolfy


1 Answers

It works for me (pg-9.1)

CREATE TABLE one
    ( one INTEGER
    );
INSERT INTO one(one) values ( 0), (1), (NULL);

SELECT case one 
        when 1 then 'one'
        when 0 then 'zero'
        else 'other' 
       end AS the_one
FROM one;

So, the single quotes (that you used to quote the aliased column name) should have been double quotes (or absent).

The result:

CREATE TABLE
INSERT 0 3
UPDATE 3
 the_one 
---------
 one
 zero
 other
(3 rows)
like image 162
wildplasser Avatar answered Mar 06 '23 06:03

wildplasser