Function Friday – Coalesce

barretblake

Barret Blake

Posted on October 26, 2022

Function Friday – Coalesce

I had intended to cover the XML XPath function this week. But due to some time constraints I needed to make this a quick hit this week, so I’m going over the coalesce function instead.

The coalesce function is quite simple. You pass in any number of arguments and coalesce returns the first item in that list that isn’t a null value.

coalesce(<item1>, <item2>, <item3>, ...)
Enter fullscreen mode Exit fullscreen mode

The function looks at each item in the order that they are passed in and whatever the first one that isn’t null is will be your output value. You can pass in variables or static values.

It’s important to remember that empty is not the same thing as null. So a blank string (“”), an empty array ([]), or an empty JSON object ({}) are not null values. So if one of the parameters is one of those, that’s what your return value will be.

coalesce(null, "Hello", null) // returns "Hello"
coalesce("", "Hello", variableA) // returns ""
Enter fullscreen mode Exit fullscreen mode

If all parameters are null, then null is the return value.

The post Function Friday – Coalesce first appeared on Barret Codes.

💖 💪 🙅 🚩
barretblake
Barret Blake

Posted on October 26, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related