473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

simple division problem


I am trying to divide one number by another and I can't get it to
return the right value....

in general I want to see how many fruits I have, then get the number of
apples and divide the apples by that total to get the percentage of
apples

I have

Declare @Val decimal(6,2)
Set @Val=
(
(select count(*) from Fruits where fruit='apples') /* = #APPLES
/
(select count(*) from Fruits) /* = TOTAL FRUITS
)
SELECT @Val

in my actual case the apples=7 and the fruits=8

but I can only return 87 or 875 (by multiplying by 100 or 1000) but I
can't get 87.5, which is the value I need.

Jul 23 '05 #1
1 3379
On 2 Mar 2005 12:56:28 -0800, Go****@Yahoo.Com wrote:
I am trying to divide one number by another and I can't get it to
return the right value.... (snip)Declare @Val decimal(6,2)
Set @Val=
(
(select count(*) from Fruits where fruit='apples') /* = #APPLES
/
(select count(*) from Fruits) /* = TOTAL FRUITS
)
SELECT @Val


Hi Go1369,

This is the result of type handling by SQL Server. Both arguments to the
division operator are integers, therefore the result of the division is
integer as well; it only gets converted to decimal(6,2) after the
remainder is already lost.

If you cast at least one of the operators to decimal, you'll get better
results:

Declare @Val decimal(6,2)
Set @Val = (select count(*) from Fruits where fruit='apples')
/ CAST((select count(*) from Fruits) AS decimal(6,2))
SELECT @Val

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: joel | last post by:
I have a table which I want to update by dividing one field into another. The update runs with no errors, but the results come out as only a positive integer number. The datatype for the result...
1
by: Dario de Judicibus | last post by:
I wish to create two simple layouts by using only HTML, CSS and the minimum JavaScript as possible. Layouts should be "elastic" (no fixed widths and heights) and cross-browser enabled. The first...
5
by: Yodai | last post by:
Hi all! I have an int that comes with a value like 0x07fa and I have to turn it into a float value of 204.2 decimal to display it.... if I try to divide it by 10 I get bogus numbers. I presume...
13
by: RadiationX | last post by:
I have to solve the following problem:Write a program that accepts two integers, and determines if the second is a factor (is evenly divisible into) the first. Here is the code i have so far. ...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
22
by: jamestuck21 | last post by:
Hi, I'm trying to work out a binary division problem 1100 / 101010101010111 Here is what I have so far but I'm not sure if I'm doing it correctly and I'm suppose to continue the division...
1
by: Synapse | last post by:
Hello... We were asked to create a simple calculator program in our C++ subject by using loops only. i have a problem in creating a loop in the multiplication and division operation so please can...
1
Death Slaught
by: Death Slaught | last post by:
I will be showing you how to make a very simple but effective three column layout. First we will begin with the HTML, or structure, of our three column layout. <!DOCTYPE html PUBLIC...
0
by: gerry | last post by:
for anyone interested I did find a solution : .outer { float:left; border:solid 1px black; overflow:hidden: } .full {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.