about - tech blog -
wildlink.com

Wildlink's Technology Blog
An occasionally updated list of informative articles.
Pulled from our internal wiki.
ColdFusion - NVL function
2017-08-16

Problem

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

Solution

Borrowing from the Oracle nvl function, here's a very simple ColdFusion implementation of nvl. (we just kept the existing name nvl from Oracle because most of the developers were already familiar with it)

It keeps code cleaner and avoids confusing Elvis operators in an already busy view.

string function nvl(required string value1, required string value2) {
    if (trim(arguments.value1) NEQ "") {
        return arguments.value1;
    }
    return arguments.value2;
}

This is included in our common_utils.cfm file in all apps.

Usage

<div>
    The value is : #nvl(variable1, '--- Not Set ---')#
</div>
Back to the Tech Blog
Blog engine: 1.3.0