Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFIF value is NULL

Status
Not open for further replies.

rojas1mg

Programmer
Jun 15, 2004
119
US
Fellow developers. I have a form that users have to enter data on. Let's say there's 15 fields, but only 3 need be filled out for now and they can come back later and enter data as needed.
How do I write a CFIF statement that checks the length gt0 then, pass the variable through, but if it's blank, set to NULL.

Thank you very much.
 
Try this:
Code:
<cfif IsDefined("Form.Whatever") AND LEN(Form.Whatever)>
  #Form.Whatever#
<cfelse>
  NULL
</cfif>



Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
When Dream Weaver writes the code for an insert query it does something very similar to what Ecobb suggested. LEN() counts leading and trailing space. while ecobb's will work you could potentially still have " " going to the DB.

<cfif IsDefined("Form.Whatever") AND trim(Form.Whatever) neq "">
#Form.Whatever#
<cfelse>
NULL
</cfif>

len(trim(form.whatever)) would also work.

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Crap! I always forget to TRIM my LENs! ;-)
Code:
<cfif IsDefined("Form.Whatever") AND LEN([red]Trim([/red]Form.Whatever[red])[/red])>
  #Form.Whatever#
<cfelse>
  NULL
</cfif>

Thanks bombboy!!



Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
hehe me too. I usualy don't see it until 30 cfif's later though.

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
From my reading on ColdFusion, CF doesn't incorporate any null values, they're instead always treated as empty strings.
 
Correct. As far as CF variables go. CF does not return a variable with a null or undefined values like ASP or javaScript. rojas1mg is trying to insert NULL values into a database if the form value contains an empty string.

Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Yeah, that's right, I have to do that all the time with date fields if they're not required.

For all other text fields, I find that empty strings work easier, especially with a Postgres database, as I was trying to do a query like "SELECT * FROM table WHERE field != 'abc'" and if the field were null, well (null != 'abc') evaluates to "null" instead of "true", and that record is not returned in the query.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top