5

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 ;

1 Answer 1

10

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Your example using either no quotes or double quotes works in PostgreSQL 8.4.12 as well.
It was only the quoting. I tend to avoid MixedCaseIdentifiers, thus avoiding quoting. The rules: single quotes := string literals; double quotes := identifiers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.