I want two decimal places

  • Hi,

    I am struggling to get two decimal places in an AVG number

    so I wrote

    select

    cast(avg(LOS) as decimal (9,2))

    from the table1

    ---it is giving me the result 9.00

    LOS is an integer...

    not giving any values after decimal.

    Please help.

    Thanks so much,

    Hai

  • hbanerje (3/18/2011)


    Hi,

    I am struggling to get two decimal places in an AVG number

    so I wrote

    select

    cast(avg(LOS) as decimal (9,2))

    from the table1

    ---it is giving me the result 9.00

    LOS is an integer...

    not giving any values after decimal.

    Please help.

    Thanks so much,

    Hai

    Because LOS is an integer, when you take the average, it's returning an integer - so you are ending up with no decimal points.

    Try casting LOS as float *before* taking the average - you should find yourself with decimal values now. (eg. AVG(cast(LOS as float))

    -Ki

  • thanks, but I am getting tons of values after decimal. how to limit that.

  • Can you post a sample table definition and sample data? I built a test bed for this and couldn't get avg(LOS) to return anything but an integer result on my system until I forced a type conversion, so I don't know how to duplicate the results you are seeing.

    -Ki

  • Yes, it is an integer result, I want to have the average in 2 decimal places.

    Thanks,

    Haimanti

  • Let me try to tackle this from another direction. Based on the information you provided, I set up this data set:

    create table #AVGtest

    (LOS int)

    go

    insert into #AVGtest (LOS) values (8)

    go

    insert into #AVGtest (LOS) values (9)

    go

    insert into #AVGtest (LOS) values (9)

    go

    Now run the following against that data:

    select AVG(LOS)

    , AVG(cast (LOS as float))

    , CAST(avg(cast(LOS as float)) as decimal(9,2))

    from #AVGtest

    You'll get 8, 8.666666666666667, and 8.67 as your results.

    Can you give me a sample set of numbers to insert into that table that results in too many decimals for the third column?

    -Ki

  • Thanks so much it worked!!

  • You're welcome. Glad it helped!

    -Ki

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

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