Search code examples
sqloracle-databaseansi-sql

Standard SQL alternative to Oracle DECODE


Is there an ANSI SQL equivalent to Oracle's DECODE function?

Oracle's decode function is the IF-THEN-ELSE construct in SQL.


Solution

  • A CASE expression is the ANSI SQL method, of which there are 2 varieties, "simple" and "searched":

    1) Simple CASE expression:

    CASE col WHEN 1 THEN 'One'
             WHEN 2 THEN 'Two'
             ELSE 'More'
             END
    

    2) Searched CASE expression:

    CASE WHEN col < 0 THEN 'Negative'
         WHEN col = 0 THEN 'Zero'
         ELSE 'Positive'
         END