Update statement rounding off

  • Hi I have an Update SQL which is rounding off and I don't want it too.

    Both Fields are decimals and the field that is being updated is a decimal. It's rounding off to 1 instead of leaving it at .96

    *********

    UPDATE Met_PkgMetrics

    SET Pkg_Per = (Installed_Clients/Targetable_Clients)

    FROM Met_PkgMetrics

    *************

    This does not work as well

    ********

    UPDATE Met_PkgMetrics

    SET Pkg_Per = cast((Installed_Clients/Targetable_Clients) as decimal(3,2))

    FROM Met_PkgMetrics

    *************

  • Try casting both to decimal(4,3).

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I did and get this error

    Server: Msg 8115, Level 16, State 8, Line 1

    Arithmetic overflow error converting numeric to data type numeric.

    The statement has been terminated.

    ******************

    UPDATE Met_PkgMetrics

    SET Pkg_Per = cast(Installed_Clients as decimal(4,3))/cast(Targetable_Clients as decimal(4,3))

    FROM Met_PkgMetrics

  • How is this column Pkg_Per defined in the database?

    😎

  • Also, how are these defined: Installed_Clients Targetable_Clients ?

    😎

  • All three fields are Decimal

  • Doesn't tell me much, decimal what; decimal(10,3), decimal(3,2)...

    😎

  • Ok this works but is odd

    If I change the Pkg_Per to a float data type it works. Although It gives me back way to many decimal places even if I try to force it to 2.

    ***

    UPDATE Met_PkgMetrics

    SET Pkg_Per = cast((Installed_Clients/Targetable_Clients) as decimal(3,2))

    FROM Met_PkgMetrics

    Select * from Met_PkgMetrics

    ****

    As well works as money data type

  • Sorry Lynn

    You pointed me in the right direction. If I change the data type back to decimal and make it 10,2 it works.

    I wasn't aware the scale was actual decimal places.

    This works now.

    Thanks for the help

  • If Pkg_Per is defined like this: Pkg_Per decimal,

    Then I know the problem. The problem is the default definition for decimal, if not explicitly defined, is like defining as (18,0) (from BOL).

    😎

  • Hi

    Please verify the data lenth of the datatype. I am sure this is causing the problem.

    Thanks -- Vj

    http://dotnetvj.blogspot.com

Viewing 11 posts - 1 through 11 (of 11 total)

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