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

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.
drumrboy44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Leon
0
Joe WoodhousePrincipal ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Joe WoodhousePrincipal ConsultantCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Sybase Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.