round function behaviour

  • Hi Everyone,

    What is there difference between the two

    select ROUND(8493.65,0)

    8494.00

    declare @val float

    set @val = 8493.65

    select round(@val,0)

    8494

    Why is it showing decimal(s) even afer rounding in the first case.

    Also, sometimes in the below stmt, it is getting rounded as expected.

    But if iMart_Graduation take the same code and excute in another management studio,

    it is showing decimal places even though am round to 0 decimal places.

    Is there any settings in the management studio or what is wrong?

    create table emp

    (id int,

    sal float

    )

    insert emp

    select 1,8329.948

    union all

    select 2,4885.34

    declare @sql varchar(100)

    set @sql = 'select sal,round(sal*12,0) from emp'

    exec (@sql)

  • In the first instance, it is probably interpreting your constant as a decimal or numeric type.

    declare @val decimal (6,2)

    set @val = 8493.65

    select round(@val,0)

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Thanks

  • "Rounding" doesn't mean the same thing as "Formatting" and how ROUND displays a result really shouldn't matter... formatting should be done in the GUI especially on date, time, and currency types so that it can use local regional setting to determine how such things should be formatted in different areas of the world. Now, if you're outputing to a file, that's a different story. Then you can use different datatypes or even a function like STR here and there to brute force formatting. But, like I said, let the GUI do the formatting.

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

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

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