PL/SQL Case

CASE syntax

CASE 
	WHEN operator {content_operator THEN {statement;} ... ...} 
	[ELSE {statement;}...] 
END CASE ;

CASE example

DECLARE 
   var CHAR(100); 
BEGIN 
   var := 'Y'; 
   CASE var 
     WHEN 'X' THEN var:='1'; 
     WHEN 'Y' THEN var:='2'; 
     WHEN 'Z' THEN var:='3';
     ELSE var:='No data found'; 
   END CASE; 
   DBMS_OUTPUT.PUT_LINE('The output is: '||var);
END;

The output is: 2  

CASE example

STUDENT_ID FIRST_NAME LAST_NAME GENDER CITY
1 Daniel SCOTT M New York
2 Anthony SIMMONS M Chicago
3 Sophia THOMPSON F Los Angeles
DECLARE
	gen      students.gender%TYPE;
	stu      students.student_id%TYPE := 3;
	message  VARCHAR2(250);
BEGIN
	SELECT gender INTO gen from students WHERE student_id = stu;
	CASE
		WHEN gen = 'M' THEN message := 'MALE';
		WHEN gen = 'F' THEN message := 'FEMALE';
		ELSE message := 'Empty column';
	END CASE;
	DBMS_OUTPUT.PUT_LINE('Message: '||message);  
END;

Message: FEMALE