Coalesce
coalesce
accepts unlimited params, and always returns the first non-null value
- In this way, you can think of the final param as the default value.
Embracing null
allows us to easily inline defaults
- You can chain a bunch of inputs and it returns the first non-null value
- ex. imagine having a result set that shows us how much revenue was earned each year since 2010. We should define a column
coalesce(income, 0) as income
in our select statement, which will return us a 0 if there is no income for the relevant year. This is a nicer alternative than to just omitting rows without data. - ex. Lets say you have an input parameter with a default, you can the just wrap the usages of that parameters like this: coalesce (_parameter, default_value) = whatever it tests against
- ex. Imagine we have a postgres function that accepts 2 args: a
start_date
and anend_date
, and returns the amount of time that an employee worked at a company. If they currently work there still, the client passesnull
for the value of theend_date
. In our function, we would have a smart failover that defaults the null value tonow()
. - ex. imagine we have a column
discount
that defaults tonull
. We want to use thisdiscount
column when determining subtotal, so we need to use coalesce to provide us with a default value of 0:
SELECT
product,
(price - COALESCE(discount,0)) AS net_price