Get a Percentage from Integers

  • I have two integers that I need a percentage number from such as

    50/5000 = 5.00%

    I Know I ought to Cast out the datatype of the percentage etc... I do not remember what the formatting was... and didn't see it in my former posts many thanks for your help...

    -- Francisco

  • First point is that the result of your example equates to 1%, not 5%

    Secondly, there is no explicit 'percentage' datatype. Not that it matters, as

    select 50 / cast(5000 as float)

    returns 0.01 and

    select 100 * 50 / cast(5000 as float)

    returns 1.0.

    Regards

    Phil

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • awesome, thanks for the answer...

    I found out I can also use a + .00 so my select would include

    (Field1 + .00) / (Field2 +.00) and that also helped to give the desired value add a * 100 and convert to decimal 2,2 with the appended '%' and it all looks good to mgmt

    your help is much appreciated

    -- Francisco

  • You may experience some "wierd" answers when using FLOAT because it does it's math in a binary rather than decimal fashion.

    --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)

  • Thanks Jeff - do you have any examples of this?

    Phil

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

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

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