Postgres SQL, Conditions using CASE

The SQL CASE expression is a generic conditional expression, similar to if/else statements in other programming languages:

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

Example of CASE usage:

SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END
FROM test;

a | case
---+-------
1 | one
2 | two
3 | other

Replication status are displayed as "f" and "t". To convert them to numeric, use the below statement:

SELECT case when pg_is_in_recovery()='f' then 0 else 100 end;
case
------
0
(1 row)

Be the first to comment

Leave a Reply

Your email address will not be published.


*