Kris
Kris

Reputation: 956

T-SQL round special cases

I need a way to round in T-SQL (SQL Server 2005).

0.0041 -> 0.005
0.0049 -> 0.005
0.0040 -> 0.004

I have tried the following select with ROUND():

SELECT ROUND(0.004, 3,1) + .001

The problem is: it doesn't work with 0.0040 -> 0.004

Upvotes: 0

Views: 93

Answers (2)

etsa
etsa

Reputation: 5060

Try this:

SELECT X 
, CEILING(X * 1000) / 1000 AS X4
FROM (
SELECT 0.0041 X UNION ALL 
SELECT 0.0049 UNION ALL 
SELECT 0.0040 ) A

Output:

X                                       X4
--------------------------------------- ---------------------------------------
0.0041                                  0.005000
0.0049                                  0.005000
0.0040                                  0.004000

Upvotes: 1

Giorgos Betsos
Giorgos Betsos

Reputation: 72165

This:

SELECT CEILING(1000 * 0.0041) / 1000.0

produces:

0.005

whereas this:

SELECT CEILING(1000 * 0.004) / 1000.0

produces:

0.004

Upvotes: 2

Related Questions