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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With