Havng trouble with division - How do I avoid integer division? URGENT

Posted on 2005-03-09
Medium Priority
Last Modified: 2012-06-27
Basically the title said it, i do a regular divide and keep getting integers, here's an example:

SELECT d.DonorType, d.DonorName, SUM(c.Amount) as YTD,
(SUM(c.Amount)/d.ProjContrib)*100 as Percent
FROM Donors d, Contributions c
WHERE d.DonorName = c.DonorName
GROUP BY d.DonorType, d.DonorName

Please any help would be greatly appreciated.
Question by:drumrboy44
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
  • 2
LVL 29

Expert Comment

ID: 13499936
What are the data type for Amount and ProjContrib fields?

LVL 24

Accepted Solution

Joe Woodhouse earned 2000 total points
ID: 13501368
This is due to the Sybase hierarchy of datatypes. I'm guessing that at least one of c.Amount or d.ProjContrib are integers.

You can promote an expression's overall datatype by prefixing with the datatype you want. That's easier to show than to explain:

SELECT d.DonorType, d.DonorName, SUM(c.Amount) as YTD,
100.0*(SUM(c.Amount)/d.ProjContrib) as Percent
FROM Donors d, Contributions c
WHERE d.DonorName = c.DonorName
GROUP BY d.DonorType, d.DonorName
LVL 24

Expert Comment

by:Joe Woodhouse
ID: 13502104
One more thing I should explain... I originally wrote the expression as

100.0*SUM(c.Amount)/d.ProjContrib as Percent

ie. without parentheses, but that risks overflow. In that case, you could wrap some or all of the expression with an explicit convert(numeric([...]), [...]), but that gets messy.

Here, it was convenient that you were already multiplying by 100, and we could just rewrite 100 as 100.0 to make it clear we meant a numeric value rather than an integer.

But if you hadn't done that, you can still do this trick by prefixing with "1.0*[...]". Doesn't change the value of the expression, but it forces a numeric datatype.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introducing Priority Question, our latest feature.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

762 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