jeudi 5 avril 2018

Error writing WHEN THEN to IF THEN

The following WHEN THEN works well in PostgreSQL,

SELECT
      CASE WHEN ab.col1='NULL' THEN ab.col2  ELSE ab.col1 END as col
FROM table1 as ab

I need to have ELSE IF in the statement, so I tried converting to a simple IF THEN as follows,

SELECT
      IF ab.col1='NULL' THEN ab.col2  ELSE ab.col1 END IF as col
FROM table1 as ab

But this gives an error,

ERROR:  syntax error at or near "."
LINE 2: IF ab.col1='NULL' then ab.col2 ELSE ab.col1...
             ^
SQL state: 42601
Character: 13 

Can you please help me to sort this out?

Aucun commentaire:

Enregistrer un commentaire