user9969
user9969

Reputation: 16060

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

Upvotes: 0

Views: 423

Answers (1)

Adarsh Shah
Adarsh Shah

Reputation: 6775

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

Upvotes: 1

Related Questions