CFLib.org – Common Function Library Project

NullColumn(columnValue [, dataType])

Last updated September 20, 2002

Version: 1 | Requires: ColdFusion 5 | Library: DatabaseLib

 
Rated 0 time(s). Average Rating: 0

Description:
This function takes a CF variable and optionally a CF datatype ('alpha' or 'numeric') and returns either the CF value or NULL. If it returns the CF value, it will be quoted if invoked with the 'alpha' datatype argument (default).

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>
<cfset testFld1 = "a">
update test<br>
set testfld = #nullColumn(testFld1)#<br>
where keycol = 2<br><br>
<cfset testFld2 = "">
update test<br>
set testfld = #nullColumn(testFld2)#<br>
where keycol = 2<br><br>
<cfset testFld3 = 9>
update test<br>
set testNumeric = #nullColumn(testFld3, 'numeric')#<br>
where keycol = 2<br><br>
</cfoutput>

Parameters:

Name Description Required
columnValue The value to test. Yes
dataType Allows you to specify 'alpha' or 'numeric'. If alpha, value is wrapped in single quotes. Default is alpha. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Useful in constructing SQL statements that must handle empty strings as NULLs.
 * Rewritten to use one UDF by RCamden
 * 
 * @param columnValue      The value to test.  (Required)
 * @param dataType      Allows you to specify 'alpha' or 'numeric'. If alpha, value is wrapped in single quotes. Default is alpha. (Optional)
 * @return Returns a string. 
 * @author Charles McElwee (cmcelwee@etechsolutions.com) 
 * @version 1, September 20, 2002 
 */

function NullColumn(columnValue) {
    var dataType = "alpha";
    
    if(arrayLen(arguments) gte 2) dataType = arguments[2];
    if(trim(columnValue) eq ""return "NULL";
     else if(dataType is "alpha"return "'" & columnValue & "'";
    else return columnValue;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
10 day(s) ago

Will Belden Will Belden added
longTime
15 day(s) ago

James Sleeman James Sleeman added
quickSort
25 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
28 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson