473,239 Members | 1,608 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,239 software developers and data experts.

How can I show decimals after division update


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!
Jul 20 '05 #1
15 50948
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" <an*******@devdex.com> wrote in message
news:40*********************@news.frii.net...

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!

Jul 20 '05 #2

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
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

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

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
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)
Jul 20 '05 #6
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).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #7
joel <an*******@devdex.com> wrote in message news:<40*********************@news.frii.net>...
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

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

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
Jul 20 '05 #8
> >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:ai********************************@4ax.com... 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)

Jul 20 '05 #9

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #10
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #11
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).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #12
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #13
On 29 Apr 2004 17:57:18 GMT, Josh Phillips wrote:
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

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


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!

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #14
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #15
On 29 Apr 2004 17:57:18 GMT, Josh Phillips wrote:
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

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


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!

Best, Hugo
--

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

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

Similar topics

4
by: Bill | last post by:
I have some values that I want to display as percent, such as the retail price/wholesale price. In some instances, the wholesale price is zero, so I get a division by zero error. What can I do...
7
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...
2
by: MLH | last post by:
An attached table named tblCustomers (but without the capital C) is attached to my Access database. The table is a remote MySQL table. I can read it and even make some changes to its data manually...
0
by: FST | last post by:
Hello All, Why does the database update from inside the Button1_Click event, but won't update from inside the DataGrid1_CurrentCellChanged event when I change cells? The message from inside the...
5
by: cover | last post by:
I have an input form that passes data when submitted to a second form to let the user know what they have just entered into the db. My question comes with using 'update'. I'd like to query the...
5
by: heddy | last post by:
I have a listbox that gets populated in code by a reader result set from a stored proc on SQL Server 2005. In building this I created a set of test rows in the DB and the code runs fine and...
12
by: CNiall | last post by:
I am very new to Python (I started learning it just yesterday), but I have encountered a problem. I want to make a simple script that calculates the n-th root of a given number (e.g. 4th root of...
5
by: question.boy | last post by:
I am trying to control the visibility of a set of div based on selected values but can't seem to get it right. Below is what I have so far but it does not work at all. The basic concept was to...
10
by: truptivk | last post by:
Hello all, I have a situation and I'd appreciate it if any of you could help me. I am not sure if this is js or html but here goes. I have a main combo box (mcb) which I'm displaying some values....
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.