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

x
Solved

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

Posted on 2005-03-09
Medium Priority
423 Views
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.
0
Question by:drumrboy44
• 2

LVL 29

Expert Comment

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

Leon
0

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
0

LVL 24

Expert Comment

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.
0

## Featured Post

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