P: n/a

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!  
Share this Question
P: n/a

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_dadb_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!  
P: n/a

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!  
P: n/a

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!  
P: n/a

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!  
P: n/a

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 email address)  
P: n/a

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 noninteger 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 email address)  
P: n/a

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  
P: n/a

> >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 200E1/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 email address)  
P: n/a

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!  
P: n/a

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!  
P: n/a

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 noninteger 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 email address)  
P: n/a

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!  
P: n/a

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 nonNULL 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 email address)  
P: n/a

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!  
P: n/a

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 nonNULL 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 email address)   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 49133
 replies: 15
 date asked: Jul 20 '05
