GitHunt
BR

brsnik/boolif-postgresql

A PostgreSQL function that returns a custom value if two booleans match.

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.

Input

boolif(value: true, expected: true, output: 'It works!', fallback: '')

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.

Installation

By default it will be added to the public schema.

Usage

With default fallback:

  • SELECT boolif(true, true, 'It works!'); returns 'It works!'
  • SELECT boolif(false, true, 'It works!'); returns NULL

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!'

Languages

PLpgSQL100.0%

Contributors

Latest Release

1.0September 23, 2021
Created September 8, 2021
Updated May 14, 2022
brsnik/boolif-postgresql | GitHunt