decimal numbers in t-sql

  • Hi All

    i an having this issue pls help

    i need to find percentage using this formula

    PERCENTAGE = (@ROD_INCIDENTS/TOTAL_INCIDENTS)*100*100

    --([ROD incidents] / [total no of incidents]) x 100 = 0.89 x 100 = 89%

    DECLARE @AA AS varchar(10)

    SET @AA = convert(varchar(10), CONVERT(decimal(10,7),33/37))

    PRINT @AA (result should be 0.07) but i am getting 0.0000000

    i need the decimal value so i can perform my percentage

    Cheers

  • Try this

    DECLARE @AA AS varchar(10)

    SET @AA = convert(varchar(10), CONVERT(decimal(10,7),33.0000000/37.00000000))

    PRINT @AA



    Clear Sky SQL
    My Blog[/url]

  • isnt this enough?

    DECLARE @AA AS varchar(10)

    SET @AA = convert(varchar(10), 33/37.0)

    select @AA

    as both numbers are integers the output also would be integer. so either one number can be converted to decimal using convert (one last bracket should close after 33)

    or multiplied with a decimal (for eg 1.0) and then devided

    To restrict the deciamls also you can use convert to decimal

  • Hey everyone,

    This topic seems to be the closest to what I'm looking for. I've got a calculation that needs to return a decimal, however, I get the same error of just returning 0.000000. What am I missing? Using the previous example:

    declare @mpr as decimal(8,8)

    set @mpr = convert(decimal(8,8), 90/407)

    select @mpr

  • Ok, got this. Each variable has to be converted.

    convert(decimal, Variable)/convert(decimal, (DateDiff("d",DateVar,DateVar)))

  • This can do

    declare @mpr as decimal(8,8)

    set @mpr = convert(decimal(8,8), 90*1.0/407)

    select @mpr

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

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