Solved

Coldfusion/MySql error - Division by zero is not possible

Posted on 2013-06-27
6
519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
mds-cos earned 500 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
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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