[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Coldfusion/MySql error - Division by zero is not possible

Posted on 2013-06-27
6
Medium Priority
?
573 Views
Last Modified: 2013-06-28
My template has a Coldfusion variable "priceperfoot" that is calculated by dividing the "price" by "totalfinishedsqft" for a home. In the database the totalfinishedsqft field can be null, 0, or a number greater than 0.  If a record has null or 0 totalfinishedsqft then I get the error Division by zero is not possible, if it has a number greater than 0, it works fine. Can someone look at my code below and tell me where the error is? Or is there a better way of handling this? Thanks!

<cfif Len(trim(TotalFinishedSQFT)) GT 0>
<cfset priceperfoot = #Price# / #TotalFinishedSQFT#>
<cfelse></cfif>
0
Comment
Question by:Bang-O-Matic
  • 3
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
mds-cos earned 2000 total points
ID: 39283242
Null is kind of funny, so I would recommend specifically checking for the null case as well as for 0:

<cfif TotalFinishedSQFT is NOT NULL>
  <cfif TotalFinishedSQFT GT 0>
...

Note that I did not use an AND to combine both comparisons.  I have run into problems when trying to do this with null values.
0
 

Author Closing Comment

by:Bang-O-Matic
ID: 39283291
<cfif TotalFinishedSQFT NEQ ''>
  <cfif TotalFinishedSQFT GT 0>

That fixed it! thanks for the help!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39283300
@mds-cos - Yeah, gotta love those null values.  BTW, CF doesn't really have the concept of NULL's.  When the query comes back, they're handled as an empty string.  

@Bang-O-Matic

Two other options:

1) Use COALESCE in your db query to eliminate the nulls altogether:

         SELECT    COALESCE(TotalFinishedSQFT, 0) AS TotalFinishedSQFT
         FROM .....
2)  Use the val() function to convert it to 0. Then you don't have to do the extra check for an empty string.

<cfif val(TotalFinishedSQFT) GT 0>
       <cfset priceperfoot = #Price# / #TotalFinishedSQFT#>
</cfif>

The end result is the same as what mds-cos suggested, just throwing it out there as an FYI :)
0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 

Author Comment

by:Bang-O-Matic
ID: 39283321
Hi _agx_,
Thanks for your input. Is there a performance difference between any of these 3 solutions?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39283483
I ran a quick and dirty test comparing the double cfif vs the val() method, and the double cfif was a *little* slower. But it wasn't really noticeable until ~10k+ iterations - and even then it wasn't much. I wouldn't worry about it unless you're performing tens of thousands of comparisons.
0
 

Author Comment

by:Bang-O-Matic
ID: 39284337
Thanks _agx_
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question