Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

The deadly train derailment that occurred recently in DuPont, Washington, raises a lot of questions. It was a new route, the first trip tested with passengers, and the train was travelling at 50 mph over the zone’s speed limit. Could IoT play a role…
The article explains the process to deploy a Self-Service password reset portal I developed a few years ago. Hopefully, it will prove useful to someone.  Any comments, bug reports etc. are welcome...
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

580 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