Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I show decimals after division update

0 views
Skip to first unread message

joel

unread,
Apr 20, 2004, 4:07:08 PM4/20/04
to

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 field is float.
The datatype for the other fields are int.

I have tried testing by dividing 7 by 10000, which should give me
0.0007. The result in the database is 0.

How do I define a field to show the full value with decimal positions?

If I enter .0007 into the field through the Enterprise Manager, it shows
0007. When I update by dividing, it shows 0.

Any help would be welcome.

Joel


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Dan Guzman

unread,
Apr 20, 2004, 11:42:40 PM4/20/04
to
When you divide an integer by an integer, the result will also be an
integer:

SELECT 23/20 --result 1

When you divide an integer by an decimal type, the result will be decimal:

SELECT 23/20.0 --result 1.1500

The above behavior is known as data type precedence. See the SQL 2000 Books
Online <tsqlref.chm::/ts_da-db_2js5.htm> for more information.

Also, note that float and real are approximate data types. Some decimal
values cannot be represented so you'll end up with close, but not always
exact, results with these. Use decimal if you need to store exact values.

SELECT CAST(23/20.0 AS float) --result 1.1499999999999999

--
Hope this helps.

Dan Guzman
SQL Server MVP

"joel" <anon...@devdex.com> wrote in message
news:408582eb$0$203$7586...@news.frii.net...

joel

unread,
Apr 21, 2004, 12:27:08 PM4/21/04
to

Thanks for your response.

The problem is that I am dividing a decimal(actually a float type) by a
decimal(float type) and still getting only integer results.

I tried exactly the same calculation from an Access database linked to
the same SQL database and got the correct result.

When I try doing it in Enterprise Manager or SQL Query Analyzer, I only
get integer results. Since most of the calculations are less than zero,
I get zero.

I have tried doing the calculation in Enterprise Manager where the
result is an integer, and I get the right result.

Am I wrong in believing that if I divide 300 by 650 I should get 0.4615
et al? I think I should, but this doesn't happen when I do the
calculation in SQL, but it does happen when I do the calculation using
Access linked to the SQL database.

Why????

joel

unread,
Apr 21, 2004, 12:27:10 PM4/21/04
to
Dan,

I just tried your cast statement and it worked. I then tried it
reversed 20/23.0 and it worked.

However, if I leave out the .0 as part of the statement, it gives me 0.
That is, if I divide 20/23 I get 0. If I put in a .0 for any of the
values 20.0/23 I get .869565.

This works even if I don't use the CAST statement.

Neither of the values in my table have a decimal position. I have
defined the column datatype for both as float. How can I get the divide
to work for these fields? It seems that I need to have the decimal (.)
as part of one of the values to get a decimal result.

Thanks

Hugo Kornelis

unread,
Apr 21, 2004, 3:25:13 PM4/21/04
to
On 21 Apr 2004 16:27:10 GMT, joel wrote:

>Dan,
>
>I just tried your cast statement and it worked. I then tried it
>reversed 20/23.0 and it worked.
>
>However, if I leave out the .0 as part of the statement, it gives me 0.
>That is, if I divide 20/23 I get 0. If I put in a .0 for any of the
>values 20.0/23 I get .869565.
>
>This works even if I don't use the CAST statement.
>
>Neither of the values in my table have a decimal position. I have
>defined the column datatype for both as float. How can I get the divide
>to work for these fields? It seems that I need to have the decimal (.)
>as part of one of the values to get a decimal result.
>
>Thanks
>
>Joel

Hi Joel,

Please post the create table statement for the tables used in the
calculation and the text of the actual query. That makes it a lot
easier to answer the question.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Edgar J

unread,
Apr 21, 2004, 5:58:17 PM4/21/04
to
joel <anon...@devdex.com> wrote in message news:<4086a0dd$0$197$7586...@news.frii.net>...


Hi Joel,

You need to convert either your dividend or divisor to a float
datatype to get your desired result. Here is an example:

SELECT convert(float, 20) / 23 AS float) -- result: 0.86956521739

Good luck!

Edgar

Dan Guzman

unread,
Apr 21, 2004, 9:28:40 PM4/21/04
to
> >However, if I leave out the .0 as part of the statement, it gives me 0.
> >That is, if I divide 20/23 I get 0. If I put in a .0 for any of the
> >values 20.0/23 I get .869565.

When you leave out the .0, you are doing integer arithmetic so the result is
an integer (no decimals). When one of the operands is a decimal type rather
than integer (i.e. 20.0 instead of 20), the result is decimal and that is
why you have a decimal scale in the result. As Edger said in his response,
at least one of the expressions needs to be a float if you want a float
result. This is because float has a higher precedence than integer or
decimal types:

SELECT 20/23 --integer
SELECT 20.0/23 --decimal
SELECT 200E-1/23 --float

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:aiid80duntu14n98l...@4ax.com...

joel

unread,
Apr 22, 2004, 9:57:10 AM4/22/04
to

Dan,

Thanks for your help. I kept looking at the datatypes of the resulting
columns and forgot to look at the datatypes of the columns used for the
division. These were int.

I changed them to decimal and I got the results I expected.

I didn't have this problem with Access, which is why I was so dense in
figuring it out.

Again, thanks for your help

joel

unread,
Apr 22, 2004, 9:57:12 AM4/22/04
to
Hugo,

I started to list the columns in the table with their datatypes and as I
was doing this I realized that the datatypes for the fields I am
dividing are "int".

I changed them to decimal and it solved the problem.

If I hadn't gone through the exercise of listing the columns and their
datatypes I might have taken a lot longer to realize the problem.

I never had this problem with Access, which is why I did not recognize
the solution.

Thanks,

Hugo Kornelis

unread,
Apr 22, 2004, 3:40:04 PM4/22/04
to
On 22 Apr 2004 13:57:12 GMT, joel wrote:

>Hugo,
>
>I started to list the columns in the table with their datatypes and as I
>was doing this I realized that the datatypes for the fields I am
>dividing are "int".
>
>I changed them to decimal and it solved the problem.
>
>If I hadn't gone through the exercise of listing the columns and their
>datatypes I might have taken a lot longer to realize the problem.
>
>I never had this problem with Access, which is why I did not recognize
>the solution.
>
>Thanks,
>
>Joel

Hi Joel,

You don't need to change the datatype just to get the division
correct. If the columns themselves contain integer data, change them
back to int.

The division will also work correctly if you write

UPDATE ....
SET FloatCol = CAST(IntCol1 AS float) / IntCol2
WHERE ....

SQL Server will switch to non-integer division as soon as one of the
operands in of a float or decimal type. The cast explicitly converts
the IntCol1 to float before the division is carried out. Without the
cast, the division would have been made first (as integer division,
thus losing all fractions) and converted to float afterwards (to store
the result in FloatCol).

Josh Phillips

unread,
Apr 29, 2004, 1:57:18 PM4/29/04
to
Hugo,

I have been reading this thread and I am running into a similar problem.
I am using the CAST but I am still getting a return of 0.0 any help
would be greatly appreciated. Here is my code:

select cast(count(SafetyTrainingYN) AS FLOAT)
from ICPCDATA
where SafetyTrainingYN = 1 /
(select count(SafetyTrainingYN)YES
from ICPCDATA)

remove _nospam_ for my email

Hugo Kornelis

unread,
Apr 29, 2004, 4:14:53 PM4/29/04
to

Hi Josh,

I can't test right now, but it looks like a parethesis problem.

(I assume the extra "YES" is a typo? Otherwise, this should give you a
syntax error!)

The server will count the number of non-NULL values in
SafetyTrainingYN in ICPCDATA. It will then calculate 1 / that number,
as integer division (yielding 0). Next, it will find rows in ICPCDATA
with SAfetyTrainingYN = that result (0). Apparently, there are none,
so the count(..) yields 0. This is converted to float and the result
is given to you: 0.0

What you probably meant is:

select (select cast(count(SafetyTrainingYN) AS FLOAT)
from ICPCDATA
where SafetyTrainingYN = 1) /
(select count(SafetyTrainingYN)
from ICPCDATA)

By the way, why don't you use 'Y' and 'N' instead of 1 and (I think) 2
to represent yes and no? You even have Y and N in the columns' name!

0 new messages