Oracle PL/SQL - PL SQL Statement CASE statements

Introduction

A CASE statement can replace code with multiple ELSIF statements.

case <selector> 
     when <valueA> then 
             ...<<set of statements>>... 
     when <valueB> then 
             ...<<set of statements>>... 
     else 
             ...<<set of statements>>... 
end case; 

Demo

SQL>
SQL> create or replace function f_getDateType_tx (in_dt DATE)
  2  return VARCHAR2-- from  w  w  w . j  ava 2 s .  c  o m
  3  is
  4       v_out_tx VARCHAR2(10);
  5  begin
  6       case to_char(in_dt,'d')
  7            when 1 then
  8                 v_out_tx:='SUNDAY';
  9            when 7 then
 10                 v_out_tx:='SATURDAY';
 11            else
 12                 v_out_tx:='WEEKDAY';
 13       end case;
 14       return v_out_tx;
 15  end;
 16  /

Function created.
SQL> begin
  2     DBMS_OUTPUT.put_line(f_getDateType_tx(SYSDATE));
  3  end;
  4  /
SATURDAY

PL/SQL procedure successfully completed.

SQL>

Related Topic