Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Panagiotis.

Asked: January 29, 2026 - 8:29 am UTC

Last updated: February 06, 2026 - 4:05 pm UTC

Version: 23ai

You Asked

Hello i just wanted to ask you the (CASE WHEN THEN END;) is the ternary operator in oracle sql - pl/sql? thanks. I provided some example code above of java and pl/sql thanks!.

/*plsql code*/
DECLARE 
myage NUMBER := 28;
statusage VARCHAR(200) := NULL;
BEGIN
statusage := CASE WHEN myage >= 70 THEN 'third age!'
WHEN myage < 70 AND myage >=50 THEN 'second age!'
ELSE 'first age!' END;
DBMS_OUTPUT.PUT_LINE('I am ' || myage || ' years old and i am in the ' || statusage);
END;


// Java code
class Main 

{
    
public static void main(String[] args) 
    
{

int myage = 28;
String statusage = null;
statusage = (myage >= 70) ? "third age!" : (myage < 70 && myage >=50) ? "second age!" : "first age!";
System.out.println("\nI am " + myage + " years old and i am in the " + statusage);
    
}

}

and Chris said...

There is no ternary operator in SQL or PL/SQL. Case expressions are one way to do this. You could also use IF statements in PL/SQL.

Decode in SQL is probably closer in spirit to a ternary operator than CASE:

var myage number;
exec :myage := 28;
select decode ( 
  :myage > 70, 
  true, 'third age!', 
  decode (
    :myage < 70 AND :myage >=50, 
    true, 'second age!', 
    'first age! ' 
  )
);

DECODE(:MYA
-----------
first age! 

More to Explore

DBMS_OUTPUT

More on PL/SQL routine DBMS_OUTPUT here