http://coldfusion.com logo
#cfml-general
Title
# cfml-general
s

Simone

03/30/2022, 7:48 PM
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

Myka Forrest

03/30/2022, 7:56 PM
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

Michael Gillespie

03/30/2022, 8:40 PM
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

Myka Forrest

03/30/2022, 8:43 PM
@Michael Gillespie, couldn't you just use
len(trim(form.id)) eq 0
and not bother with the
iif(...)
?
m

Michael Gillespie

03/30/2022, 8:45 PM
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

Simone

03/30/2022, 9:06 PM
or i can just use
VAL
m

Myka Forrest

03/30/2022, 9:06 PM
if
form.id
isn't defined,
val()
wouldn't work
m

Michael Gillespie

03/30/2022, 9:14 PM
@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

cfvonner

03/31/2022, 3:53 PM
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.