Looking for a little help with this code. Without posting the entire file which is way to big I just need a little help with using Nullif in Coldfusion.
I could I guess use it in my SQL statment, but for the sake of learning I am wondering if it can be used when setting variables as follows :-
The code doesn't throw any errors but I'd like to know where I would place the 0 after the Nullif.
<cfif AE_C NEQ 0>
<cfset AE_P=AE_T/AE_C>
<cfset AE_A=AE/AE_C*100>
<cfset AE_B = AE-AE_C/H8*H9>
<cfset AE_D=AE/H9*H8>
<cfelse>
<cfset AE_P=ISNULL(AE_T/NULLIF(AE_C))>
<cfset AE_A=ISNULL(AE/NULLIF(AE_C*100))>
<cfset AE_B=ISNULL(AE-AE_C/NULLIF(H8*H9))>
<cfset AE_D=ISNULL(AE/NULLIF(H9*H8))>
</cfif>
Hoping it can be done this way.
IMPORTANT: Your code is not showing any error because
ISNULLis masking the error.Also
NULLIFis not a valid ColdFusion function. I believe the reason why there is no error in your page because, ColdFusionISNULL()function seems to be a very versatile one and showing some undocumented characteristics.ISNULL()does not return an error even if the expression inside it is defined or not if the expression is syntactically valid.eg.
What you could do as an alternative is the following.
The following is a bit hacky, but you can check out the function
val(). It will return 0 for any string that is not a number (check the doc for more details).NULLIF(AE_C)becomesval(AE_C).Still if the
val()return0, then the output ofISNULL()will beYES, because division by0throws error.