Standard SQL alternative to Oracle DECODE

JavaRocky picture JavaRocky · Jul 9, 2010 · Viewed 14.3k times · Source

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

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

Answer

Tony Andrews picture Tony Andrews · Jul 9, 2010

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