Function Friday – Coalesce

Photo by Tima Miroshnichenko from Pexels

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>, ...)

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 ""

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

Leave a Reply