NexusDB Manual V3 > SQL Reference > Value Expressions
Specify a conditional value.
Syntax
<case expression> ::= <case abbreviation> ::=
<case specification> ::=
<simple when clause> ::= WHEN <when operand> THEN <result>
<searched when clause> ::= WHEN <search condition> THEN <result>
<else clause> ::= ELSE <result>
<case operand> ::= <row value predicand>
<when operand> ::= <row value predicand>
<result> ::=
Usage
The CASE expression is similar in concept to the CASE statement found in some programming languages. It is used in SQL to provide a conditional value, and can be specified anywhere a value expression is allowed.
Notes
CASE WHEN value1 = value2 THEN NULL ELSE value1 END
CASE WHEN value1 IS NOT NULL THEN value1 WHEN value2 IS NOT NULL THEN value2 WHEN valuen IS NOT NULL THEN valuen ELSE NULL END
Tip: Use COALESCE in arithmetic expressions to avoid calculations on a null value.
Examples
SELECT studentID, studentName, CASE WHEN gender = 'F' THEN 'Female' WHEN gender = 'M' THEN 'Male' ELSE 'Not available' END AS gender_description FROM students
SELECT studentID, studentName, CASE gender WHEN 'F' THEN 'Female' WHEN 'M' THEN 'Male' ELSE 'Not available' END AS gender_description FROM students
SELECT orderID, NULLIF( orderTotal, 0 ) AS orderTotal FROM orders ORDER BY orderID
SELECT orderID, COALESCE( orderTotal, 0 ) - COALESCE( amountPaid, 0 ) AS "Amount Due" FROM orders ORDER BY orderID
Conformance
|
© Nexus Database Systems Pty Ltd.
