473,839 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 51065
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.1499999999999 999

--
Hope this helps.

Dan Guzman
SQL Server MVP

"joel" <an*******@devd ex.com> wrote in message
news:40******** *************@n ews.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(actuall y 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*******@devd ex.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_rFa ct.in_SPAM_fo> wrote in message
news:ai******** *************** *********@4ax.c om... 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

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

Similar topics

4
7957
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 to avoid this? Also, how can I get this to only show two decimals, instead of it going .##### the way it does. I want it to look like .45% Thanks a million,
7
7712
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 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.
2
1627
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 in table view. I would like an example PassThrough query's SQL to show how to update . to True. Thanks.
0
1224
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 DataGrid1_CurrentCellChanged event appears but OleDbDataAdapter1.Update(DataSet11) doesn't happen. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
5
2203
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 database by equipment number (equipno is unique) and query all fields from that row, populating an original form 'look alike' whereby the user can add detail to the original records as the db builds. I'm using 'date' twice because I'd like to...
5
2302
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 populates the box. Next I added functionality to add data. This adds data to the DB and then forces the listbox to refresh by clearing it's contents and then re-running the populate code. However, when I add an item and do the refresh, the listbox...
12
2107
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 625--obviously five, but it's just an example :P), and because there is no nth-root function in Python I will do this with something like x**(1/n). However, with some, but not all, decimals, they do not seem to 'equal themselves'. This is...
5
2980
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 have a set of Divs A through Z and by click the associated text character that Div would be visible and all others would be hidden. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://...
10
7137
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. Based on what the user chooses, 2 combo boxes should be displayed on same page. The first combo box (CB1), should be filled with some values. The second, (CB2) however, should get loaded on basis of what is chosen in CB1. UNtil then, CB2 should be...
0
9854
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9696
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10645
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10290
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9425
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4482
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3131
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.