# Thread: Integer / Integer

1. Registered User
Join Date
Jul 2007
Posts
7

## Unanswered: Integer / Integer

Hi All,

If I run "SELECT 610/100", query analyser returns 6! (wrong)

If I run "SELECT 610.0/100", query analyser returns 6.100000! (correct)

I know I can get the correct result by running "SELECT Cast(610.0 as Float)/100", but why doesn't the first example return 6.1?

Is there some setting on my server which says Integer / Integer = Integer ?

Regards

Xo

2. King of Understatement
Join Date
Feb 2004
Location
One Flump in One Place
Posts
14,912
That's integer division. Not a server setting. BTW - I would be cautious casting to float, depending on what you are doing of course.

3. www.gvee.co.uk
Join Date
Jan 2007
Location
UK
Posts
11,445
Computers are stupid - remember that
You have to be explicit and watch out when using approximate data types (float).

Using ".0" implicitly converts it to a decimal; hance a decimal result.

4. Registered User
Join Date
Sep 2005
Posts
161
Another approach:

select (col1*1.0)/(col2*1.0)

5. King of Understatement
Join Date
Feb 2004
Location
One Flump in One Place
Posts
14,912
Originally Posted by cascred
Another approach:

select (col1*1.0)/(col2*1.0)
You only need one input to be a decimal to get decimal returned, so select 1.0*col1/col2 works fine too.

6. Resident Curmudgeon
Join Date
Feb 2004
Location
In front of the computer
Posts
15,579
The original poster should probably read about Transact-SQL expressions, especially the part about type conversion.

-PatP

7. Window Washer
Join Date
Nov 2002
Location
Jersey
Posts
10,322
And why bother to even have a datatype of float if it gives you the wrong answer?

8. King of Understatement
Join Date
Feb 2004
Location
One Flump in One Place
Posts
14,912
Because it doesn't. The problem is 99% of the time people use float they are asking the wrong question.

9. Window Washer
Join Date
Nov 2002
Location
Jersey
Posts
10,322
Originally Posted by pootle flump
Because it doesn't. The problem is 99% of the time people use float they are asking the wrong question.

For example?

10. King of Understatement
Join Date
Feb 2004
Location
One Flump in One Place
Posts
14,912
Heh. We've been here before.
Originally Posted by Brett Kaiser
For example?
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=71391 (Check out MVJ's posts in this thread - they are probably the most illuminating few lines on the topic I have read anywhere about floats - and it includes two examples ).

11. Registered User
Join Date
Sep 2005
Posts
161
Originally Posted by Brett Kaiser
And why bother to even have a datatype of float if it gives you the wrong answer?
You need float if you are going to represent very large or very small numbers. What else can you use to represent 10E+100?

12. Window Washer
Join Date
Nov 2002
Location
Jersey
Posts
10,322
Great stuff

13. Resident Curmudgeon
Join Date
Feb 2004
Location
In front of the computer
Posts
15,579