IF

Purpose

The IF THEN ELSE syntax is an alternate to CASE function.

Syntax

if::=

IF THEN ELSE

Usage Notes

  • If the condition evaluates to true, the expression after THEN is returned.
  • If the condition evaluates to false, the expression after ELSE is returned, or NULL if an ELSE expression is not defined.
  • If the condition evaluates to NULL/UNKNOWN, the expression returns NULL.

Examples

SELECT name, age, IF age < 18 THEN 'underaged' ELSE 'adult' ENDIF AS 'LEGALITY' FROM persons;
Result
NAME AGE LEGALITY
Alice 25 adult
Bob 16 underaged
Charlie NULL NULL
David 34 adult