简体   繁体   中英

Rounding to the nearest all number in sql server 2008

I need to write a function that will round up to the nearest whole number in sql server 2008. May be there is already that function.

When rounding I need to have 2 decimal places.

EG

If a number is less than 5 "4.4" round it to 4 if more than "5" 4.6 than round it to 5

Examples:

2.664543=2.70

4.2432=4.20

How can I do it in sql server?

many thanks

You can use ROUND function for that.

ROUND ( numeric_expression , length [ ,function ] )
SELECT ROUND(4.4, 0) -- Gives 4.0
SELECT ROUND(4.6, 0) -- Gives 5.0
SELECT ROUND(2.664543, 1)  -- Gives 2.70000
SELECT ROUND(4.2432, 1) -- Gives 4.2000

SELECT CAST(ROUND(4.2432, 1) as NUMERIC(36,2))  -- Gives 4.20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM