CASE statements : CASE « PL SQL Statements « Oracle PL/SQL Tutorial






A Traditional Condition Statement

SQL>
SQL> create or replace function f_getDateType (in_dt DATE)
  2  return VARCHAR2
  3  is
  4      v_out VARCHAR2(10);
  5  begin
  6      if to_char(in_dt,'d') = 1 then
  7          v_out:='SUNDAY';
  8      elsif to_char(in_dt,'d') = 7 then
  9          v_out:='SATURDAY';
 10      else
 11          v_out:='WEEKDAY';
 12      end if;
 13      return v_out;
 14  end;
 15  /

Function created.

A Condition Using a CASE Statement">

case <selector>
   when <valueA> then
         ...<<set of statements>>...
   when <valueB> then
         ...<<set of statements>>...
   else
         ...<<set of statements>>...
end case;
SQL>
SQL> create or replace function f_getDateType (in_dt DATE)
  2  return VARCHAR2
  3  is
  4      v_out VARCHAR2(10);
  5  begin
  6      case to_char(in_dt,'d')
  7          when 1 then
  8              v_out:='SUNDAY';
  9          when 7 then
 10              v_out:='SATURDAY';
 11          else
 12              v_out:='WEEKDAY';
 13      end case;
 14      return v_out;
 15  end;
 16  /

Function created.








22.2.CASE
22.2.1.CASE statements
22.2.2.When creating selector CASE statements, you cannot have NULL in the list of possible values.
22.2.3.Use CASE statement
22.2.4.Named case block
22.2.5.case when
22.2.6.An example of comparison of two numbers using a searched CASE expression
22.2.7.Variable assignment with case statement
22.2.8.Use case statement in a dbms_output.put_line
22.2.9.Simple CASE statement with range
22.2.10.Case statement to call procedure
22.2.11.Return statement with case
22.2.12.Use case statement in procedure call to use the proper parameter value