ColdFusion - NVL function

Article Information

Edited:

Article Content

Problem

A very common pattern is to show something else if a given variable is empty/blank/null.

Solution

Originally borrowing from the Oracle nvl function and expanded to copy general SQL's coalesce function, here's a very simple ColdFusion implementation. (We kept the existing name nvl from Oracle with a convenience copy in coalesce because most of the developers were already familiar with the function names)

The funciton helps keep code cleaner and avoids confusing Elvis operators or messy if chains in an already busy view.

/**
 * Returns first non-blank argument.
 * @CFLintIgnore FUNCTION_TYPE_ANY
 */
any function nvl() {
    for (var argKey in arguments) {
        if (trim(arguments[argKey]) NEQ '') {
            return arguments[argKey];
        }
    }
    return '';
}

This is included in our common_utils module available in all 5.5 or newer apps (older apps can find it in the common_utls.cfm file).

Usage

<div>
    The value is : #nvl(variable1, variable2, variable3, '--- Not Set ---')#
</div>

Our Mission

At Wildlink, our mission is to deliver the best business solutions we can to enable our clients to gather and unlock the answers in their data.
We are committed to fostering long-term partnerships built on trust, integrity, and a relentless pursuit of innovation.