?
Solved

Coldfusion/MySql error - Division by zero is not possible

Posted on 2013-06-27
6
Medium Priority
?
532 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 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
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

 

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
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…
Suggested Courses

765 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