Home All Groups Group Topic Archive Search About
Author
12 Jan 2006 7:52 PM
Paul fpvt2
When I do the following query:
select convert(decimal(8,2),(1/3))  -- > returns .00

When I do the following query:
select convert(decimal(8,2),convert(float,1)/convert(float,3)) -- > returns
..33

Is there a better way to do the division other than converting each number
to float before dividing them ?
Thank you.

Author
12 Jan 2006 7:55 PM
SQL
I usually do this select 1.0*1/3

http://sqlservercode.blogspot.com/
Author
12 Jan 2006 8:11 PM
Dennis Lam
Hi Paul,

The reason that your first SQL statement returns .00 because it treated the
number 1 and 3 as integers and "if an integer is divided by an integer, the
result is an integer that has any fractional part of the result truncated.". 
Therefore, only a integer is returned for the convert function. i.e. 0 -> .00.

So you do need to convert the numbers first unless you use this:
   Select (1.0/3.0)

Hope this help.
Dennis Lam

Show quote
"Paul fpvt2" wrote:

> When I do the following query:
> select convert(decimal(8,2),(1/3))  -- > returns .00
>
> When I do the following query:
> select convert(decimal(8,2),convert(float,1)/convert(float,3)) -- > returns
> .33
>
> Is there a better way to do the division other than converting each number
> to float before dividing them ?
> Thank you.

AddThis Social Bookmark Button