Search code examples
sql-server-2008integer-division

Issue with simple division in SQL Server


I must be a total idiot or something. I'm using Microsoft SQL Server 2008. I can't do simple division. SELECT 200/600 returns a big fat goose egg (0.00). Yet if I do 600/200 it returns 3. The only thing I can think of is some server setting. I'm feeling pretty darn stupid here. Can somebody please help?

Shown on my server


Solution

  • The numbers you are using are being interpreted as integers, hence values you might not normally expect.

    Try this...

    select 200.0/600.0
    

    and you'll see that SQL Server treats the numbers as floats and therefore gives you the result you want.

    Alternatively you could try

    select cast (200 as float) / cast (600 as float)
    

    And you'd get the same result - all I've done here is to explicitly cast the values as floats.