tsql - Rounding down decimal numbers in SQL Server 2008 -


i read rounding functions of t-sql round, floor, , ceil, none of them has rounded down decimal numbers correctly me.

i have 2 questions:

  1. how round down decimal number (3.69 ==> 3.5)?
  2. how round last 3 digits of integer (e.g. 142600 ==> 143000)?

1) select cast(floor(2 * 3.69) / 2 decimal(2, 1)) handles first case - courtesy of answer similar question on sql server forums, adapted , checked.

note if numbers rounding nearest 0.5 bigger (e.g. 333.69 => 333.5), sure specify more decimal precision when cast (e.g. select cast(floor(2 * 3.69) / 2 decimal(10, 1))), or overflow error:

msg 8115, level 16, state 8, line 1 arithmetic overflow error converting numeric data type numeric. 

extra precision not affect bottom-line result (i.e. select cast(floor(2 * 3.69) / 2 decimal(10, 1)) , select cast(floor(2 * 3.69) / 2 decimal(2, 1)) both yield 3.5); wasteful if numbers rounding smaller.

online references examples available t-sql floor, cast, , decimal help.

2) select round(142600, -3) handles second case.

a similar online reference available t-sql round.


Comments