2

I'm new to conditional statements within postgres. I have the following statement SELECT IF status = 'L' THEN edate ELSEIF status = 'C' THEN 'wrong date' END IF as date FROM campaigns;

But I get an error

ERROR:  syntax error at or near "status" at character 11
LINE 1: SELECT IF status = 'L' THEN edate ELSEIF status = 'C' THEN '...

May I use it like this or what am I doing wrong?

1 Answer 1

9

IF isn't part of SQL syntax, can't work. Use a CASE:

SELECT 
  CASE status
    WHEN 'L' THEN edate 
    WHEN 'C' THEN 'wrong date' 
  END as date 
FROM 
  campaigns;

Ps. If "edate" is of type DATE or TIMESTAMP, you have to cast this column to a VARCHAR because 'wrong date' is not a DATE nor TIMESTAMP.

Sign up to request clarification or add additional context in comments.

Comments

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.