HI, can any body plssssssssssssssssssssssssss help Hi,division question ( probably 101)
I have a table...
column1 /int / = totalOrders
column2 /int / = totalShippedOrders
column3 /Numeric (10,5) / = Percentage of orders shipped
I need to see the correct percentage but can't seem to get it correct when I use sql to update the column3
example...
col1 Col2 col3
49 40 ??????
When I try i just get weird values returned, i tried col1/col2 * 100 ...not work so good for me.
all help greatly appreciated
I need to use an update query or ?
An English in SwedenSv: division question ( probably 101)
The problem might be that you are dividing to integers, which might result in an integer division. Therefor I would look into the two functions CAST() and CONVERT(), and make sure that each column is converted to a float datatype prior to the division.
<info>
Example:
select 15/20 -- Returns 0 (integer)
select CONVERT(float,15/20) -- Returns 0.0 (float)
select CONVERT(float,15)/CONVERT(float,20) -- Returns 0.75 (float)
</info>
Also, it is generally not recommended to store redundant information in a database (you might want to look into the term "Normalization"). Since column3 can be calculated from column1 and column2 you really don't need that column, except maybe in extreme cases for performance reasons. If you remove column3, you could either calculate the percentage in you application, or create a SQL view that hold the extra column.