views:

57

answers:

2

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

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

+2  A: 
CASE WHEN a=1 THEN value1
     WHEN a=2 THEN value2
     ELSE default
END

http://stackoverflow.com/questions/4622/sql-case-statement-syntax

Rup
+8  A: 

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
Tony Andrews
To add: Oracle 9i+ supports CASE, but there's also the PLSQL CASE expression...
OMG Ponies