SQL Functions

PostgreSQL includes several functions that return values based on expressions supplied in the current SQL statement. Moreover, these functions are not constrained to acting on specific data types; rather, they act as control structures within a SQL statement.

CASE WHEN

Description

The CASE WHEN function is a simple conditional evaluation tool. Most programming languages contain similar constructs. It can be thought of as analogous to the ubiquitous IF…THEN…ELSE statement.

Inputs
CASE WHEN condition THEN result 
      [ WHEN condition THEN result ] 
     … 
      [ ELSE result ] 
END 
Example

This example shows a classic IF…THEN…ELSE paradigm in which the CASE WHEN function can be used. The age of an employee is compared against certain constants, ...

Get PostgreSQL Essential Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.