Why aren't I seeing the number that I expect?

  • Hi Everyone,

    I don't understand why I am getting all zeros after the decimal point below.

    I have two main questions -

    1). Why are there so many places after the decimal point?

    2). Why am I seeing nothing but zeros (I expected 14.46888137723487)?

    Any help will be greatly appreciated.

    Kind Regards,

    David

  • U have to specify the decimal places

    Runt the below ...U will get the expected result

    declare @dividend decimal(12,5),@divisor decimal(12,5)

    set @dividend='105.12345425'

    set @divisor='7.2654859425'

    select @dividend/@divisor

  • Hi Indu,

    Thank you for your response. Initially when I tried to specify the decimal values parameters, e.g.: DECLARE @dividend decimal(10,3), I was met with 'red underlines' in SSMS. However something unrelated was probably happening.

    At any rate now that I am able to correctly setup the variables I am able to lead into the real issue that I am facing. I really want to limit the decimal 'fidelity' to two decimal places after a division operation, however no matter what I do I can't seem to achieve the desired outcomes.

    What is happening above?

    Why after declaring decimal(10,3) am I seeing so many decimal places in my result?

    Why is a CAST statement that specifically tells both the dividend and the divisor to be two decimal places being ignored?

    How can I get a result like 14.59?

    Adding a CAST to the entire line, e.g.: CAST(@dividend / @divisor AS decimal(15,2)), causes an 'Incorrect Syntax' error!

    Any further help or suggestions will be greatly appreciated.

    Kind Regards,

    David

  • David,

    you have just specified the datatypes for the inputs.

    there are many decimals in the result ,as u haven't restricted the decimal places for output

    do the below

    declare @dividend decimal(12,5),@divisor decimal(12,5)

    declare @output decimal(12,2)

    set @dividend='105.12345425'

    set @divisor='7.2654859425'

    set @output = @dividend/@divisor

    select @output

  • Hi Indu,

    This works great for static data held in variables, however I want to achieve the same outcome using actual data contained in tables. Can you please explain how to do this 'on the fly', that is without using variables?

    Kind Regards,

    David

  • You've already defined the inputs. Cast the answer, not the parts of the formula.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • david.dartnell (9/5/2014)


    Hi Indu,

    Thank you for your response. Initially when I tried to specify the decimal values parameters, e.g.: DECLARE @dividend decimal(10,3), I was met with 'red underlines' in SSMS. However something unrelated was probably happening.

    At any rate now that I am able to correctly setup the variables I am able to lead into the real issue that I am facing. I really want to limit the decimal 'fidelity' to two decimal places after a division operation, however no matter what I do I can't seem to achieve the desired outcomes.

    What is happening above?

    Why after declaring decimal(10,3) am I seeing so many decimal places in my result?

    Why is a CAST statement that specifically tells both the dividend and the divisor to be two decimal places being ignored?

    How can I get a result like 14.59?

    Adding a CAST to the entire line, e.g.: CAST(@dividend / @divisor AS decimal(15,2)), causes an 'Incorrect Syntax' error!

    Any further help or suggestions will be greatly appreciated.

    Kind Regards,

    David

    BRACES ARE MISSING

    Below will work

    CAST((@dividend / @divisor) AS decimal(15,2))

  • BRACES ARE MISSING IN UR QUERY

    Below will work

    CAST((@dividend / @divisor) AS decimal(15,2))

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

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