naspy971
naspy971

Reputation: 1391

SQL - Select dummy field and set value with condition on another field

I neeed to do something like this in PostgreSQL:

SELECT "OFF_ID", "DUMMY" ( <--fake field )

CASE WHEN "OFF_ID" = 10661
THEN 
    DUMMY value is set to "john"
ELSE 
    DUMMY value is set to "doe"

WHERE "OFF_STATUS" = TRUE

The " CASE " block above is TOTALLY WRONG, I just can't figure out how to proceed.

In other words, I need to select a fake column (DUMMY), and set the returned value according to a condition, in my example, depending on OFF_ID value.

Upvotes: 0

Views: 724

Answers (2)

Fahmi
Fahmi

Reputation: 37483

You can try below -

SELECT "OFF_ID", 
CASE WHEN "OFF_ID" = 10661 THEN 'john' ELSE 'doe' end as Dummy
from tablename
WHERE "OFF_STATUS" = TRUE

Upvotes: 1

user330315
user330315

Reputation:

You need to give the alias "dummy" to the result of the CASE expression:

SELECT "OFF_ID", 
       CASE 
         WHEN "OFF_ID" = 10661 THEN 'john'
         ELSE 'doe'
       END AS dummy
FROM ...
WHERE "OFF_STATUS" = TRUE

Upvotes: 2

Related Questions