how can i add 0 in cfqueryparam if no value is def...
# cfml-general
s
how can i add 0 in cfqueryparam if no value is defined using a NULL attribute
Copy code
<cfqueryparam cfsqltype="cf_sql_integer" value="#form.id#" null="#NOT len(trim(form.id))#">
m
Do you mean you want to send 0 to the DB if
form.id
is not defined? The null attribute determines whether or not to send a null to the BD. If you want to send 0, the null attribute won't be used. Check your
form.id
and use the value to send
form.id
or 0.
m
If you want to send a null value when form.id = "" you can use and IIF in the null attribute. Change your condition to match what should generate a null in your database.
Copy code
<cfqueryparam cfsqltype="cf_sql_integer" value="#form.id#" null="#iif(len(trim(form.id)) eq 0,DE('true'),DE('false'))#">
m
@Michael Gillespie, couldn't you just use
len(trim(form.id)) eq 0
and not bother with the
iif(...)
?
m
not sure, didn't try it. I have been using IIF forever in this scenario - guess I should look to see if the newer versions of CF can evaluate inside the attribute.
s
or i can just use
VAL
m
if
form.id
isn't defined,
val()
wouldn't work
m
@Myka Forrest Don't stop there. There has to be at least a half dozen more ways to answer this question. Mine was the result of just happening to be in a query that did the null switch so it was a quick cut-n-paste, answered the question, showed something different - and works.
c
how can i add 0 in cfqueryparam if no value is defined using a NULL attribute
Can you clarify "no value is defined"? Do you mean the
id
key might not exist in the
form
scope? Or that it is empty? I can't think of a scenario where
form.id
would actually be NULL.