Rounding issue after decimal in TSQL

  • HI All,

    My query is like below:

    declare @id numeric(17,2) = '123.456'

    select @id

    Output : 123.46

    Expected output is : 123.45

     

    Please help me on this query.

     

  • So, you are expecting the value to be truncated and not rounded.

    Try this

    DECLARE @id numeric(17, 2) = ROUND('123.456', 2, 1)

    SELECT @id;
  • Change your precision to make the query work as you intend:

    declare @id numeric(19,6) = '123.456'

    select @id, ROUND(@id,2)

    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

  • Declare @id numeric(17,2) = round(‘123.456’,2,1)

  • No luck sir still i am getting the same result.

    declare @id numeric(19,6) = '123.456'

    select @id, ROUND(@id,2)

    (No column name) (No column name)

    123.456000 123.460000

     

    please provide any other alternative way.

  • My mistake. Try these:

    declare @id numeric(19,6) = '123.456'

    select @id, up = ROUND(@id,2,0), down = ROUND(@id,2,1)

    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

  • Great Thanks.

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

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