For the complete documentation index, see llms.txt. This page is also available as Markdown.

CASE OPERATOR

Implement conditional logic in SQL queries. This operator evaluates conditions and returns a specific value when the first true condition is met.

Syntax

CASE value WHEN [compare_value] THEN result [WHEN [compare_value] THEN
result ...] [ELSE result] END

CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...]
[ELSE result] END

The BNF documents the two CASE-operator forms (simple and searched); each gets its own diagram.

Railroad diagram of the simple CASE form
Railroad diagram of the searched CASE form

Description

The first version returns the result for the first value=compare_value comparison that is true. The second version returns the result for the first condition that is true. If there was no matching result value, the result after ELSE is returned, or NULL if there is no ELSE part.

There is also a CASE statement, which differs from the CASE operator described here.

Examples

Only the first matching condition is processed:

See Also

This page is licensed: GPLv2, originally from fill_help_tables.sql

spinner

Last updated

Was this helpful?