Skip to content

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

Notifications You must be signed in to change notification settings

brsnik/boolif-postgresql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 

Repository files navigation

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