Division corrupt?

  • So I open QA and typ:

    SELECT 330/60

    I expect the return of 5.5, I receive 5.00. When dividing 30/60 I get the value 0.0. When declaring and using variables like decimal, real or float: same result.

    Why? What am I missing?

    Greetz,
    Hans Brouwer

  • Because sql converts the numbers to ints

    so

    SELECT 330 / 60

    is the same as

    SELECT CAST(330 as int) / CAST(60 as int)

    You need to tell sql that one of the number is not an int. eg all the following return 5.5

    SELECT 330./60

    SELECT 330/60.

    SELECT CAST(330 as numeric(9,2))/60

    SELECT 330/CAST(60 as numeric(9,2))

    DECLARE @val1 numeric(9,2),@val2 numeric(9,2)

    SET @val1 = 330

    SET @val2 = 60

    SELECT @val1 / @val2

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Tnx David. 1 learns everyday.

    Greetz,
    Hans Brouwer

  • I forgot to mention the the result and it's type is based on the types used in the calculation, as above any calcualtion using two ints will produce an int.

    It is good pratice to explicitly cast values to get the right result, makes it easier for the next person.

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply