DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

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.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay