Using CASE
's is great however when there are too many conditions it can get uber complicated, this solves a very specific problem I've encountered.
Variable | Type | Required | Description |
---|---|---|---|
value | boolean | Yes | Boolean value from the database. |
expected | boolean | Yes | The expected boolean value in order to return the output . |
output | varchar | Yes | Will be returned when value and expected are a match. |
fallback | varchar | Optional | Will return NULL unless set to a specific fallback when value and expected do not match. |
By default it will be added to the public
schema.
With default fallback:
SELECT boolif(true, true, 'It works!');
returns'It works!'
SELECT boolif(false, true, 'It works!');
returnsNULL
With custom fallback:
SELECT boolif(true, true, 'It works!', 'Oh yeah!');
returns'It works!'
SELECT boolif(false, true, 'It works!', 'Oh yeah!');
returns'Oh yeah!'