views:

743

answers:

3

If one of the columns in the returned coldfusion query result set has a NULL, how do we check if the value of this column being NULL?

Should we just say <cfif queryname.columnname[i] EQ ''> OR <cfif queryname.columnname[i] eq 'NULL'> ?

+10  A: 

Null value will only be returned as empty string in a query object.

Henry
+8  A: 

Ideally, you would use your database's ifnull (or similar) method to convert null values to something appropriate before they reach CF.

If not, they come back as an empty string, so your first cfif is correct.

Peter Boughton
Agreed. If you need to distinguish a true NULL value from an empty string, you need to do that in the query.
davidcl
And sometimes I wish it didn't work like this. NULL can actually <i>mean</i> something in certain circumstances.
Ciaran Archer
Yes, it means you've done your database wrong. ;)
Peter Boughton
+1  A: 

If you are using an oracle database you could use the NVL method and modify your query to wrap the column.

<cfset var nullstringvalue = "THELASTNAMEFIELDISNULL" />
<cfquery name="lastnamelookup" ....>
  SELECT NVL(lastname, 'LASTNAMEISNULL') AS lastname
</cfquery>
<cfif lastnamelookup.lastname EQ nullstringvalue>
     ...do null case work...
</cfif>

The trick would be in determining the correct bogus string to use in this case, then testing for it.

np0x