Print (5596 / 11784) --- > yields 0 (should return .47 and not 0)

  • Removed by GaMusicMan

  • You are asking for an integer result by dividing and INT by an INT.

    Try forcing a conversion to numeric during the calculation by adding a decimal to one of the numbers.

    Print (5596. / 11784)

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • GaMusicMan (4/26/2011)


    In the SQL Server 2005 Management Studio, I have the following line of code in the query:

    Print (5596 / 11784)

    This should return the decimal .47 . Instead it returns 0.

    Can anyone tell me why this expression returns 0 instead of .47 . I'd appreciate your feedback. I'M STUCK HERE.

    Dividing an integer by an integer yields an integer result. Try casting an operand into your required datatype:

    SELECT CAST(5596 AS NUMERIC(6,2))/ 11784

    SELECT 5596/CAST(11784 AS NUMERIC(7,2))


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

  • That works. Thank you.

  • Because that an INTEGER division; Interger division will round off the result to the nearest lower integer. Try this instead

    Print (5596.00 / 11784.00)

  • Both of your suggestions worked. thanks a lot. I'm getting the hang of this T-SQL thing.... little by little...

  • 😀 3 replies 🙂 with the same answer 😉

Viewing 7 posts - 1 through 6 (of 6 total)

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