www.oradev.com   for Oracle developers
  Pl/sql articles   Oracle articles   Performance   scripts   Books   Documentation   Links   XML
  Articles
Oracle numeric functions
Number format
Sequence
dbms_lob
dbms_output
Oracle substr
Using a ref cursor
How to use the CASE statement
How to use the DECODE statement
How to use the NVL statement
Oracle training
Oracle certification
Create statistics
dbms_profiler explained
How to use hints
Autonomous transaction
Oracle date format
Oracle sysdate
Rename tables, columns
to_date function
Scheduling in 10g
How to use utl_http




  Case statement

How to use the case statement in Oracle

The CASE statement can be used in SQL for and IF-THEN-ELSE construction. It's an alternative for the decode statement and was introduced in Oracle 8. Syntax:
case( when condition then expr1 [when condition then expr2] ... [else exprN] ) end

Oracle searches for the first when condition that is true. The expr belonging the this condition is returned. If Oracle does not find such a condition, the expression belonging to the else statement is returned. If no else is specified the null value will be returned.
Sample code
select id
,      case (when status ='A' then 'Accepted'
             when status ='D' then 'Denied'
             else 'Other') end
from   contracts;

Will return for each id:
If status = 'A' : 'Accepted' 
If status = 'D' : 'Denied' 
Else            : 'Other'