The function requires 3 parameters:
- Natural Number
- Nearest Power of 10
- Direction
Simple Use Cases:
SELECT * FROM dbo.udf_RoundNearest (1264,10,0) /* The Direction 0 indicates the nearest Power of 10 round down. Result = 1260 */SELECT * FROM dbo.udf_RoundNearest (126412,1000,0) /* Result 126000*/SELECT * FROM dbo.udf_RoundNearest (126412,1000,1) /* Result 127000*/
Integrating the function with a table/view/derived table/CTE:
;with tmp (Value) as
(select 1236 union all select 6584 union all select 9999)
select t.*, fn.Number
from tmp t
cross apply dbo.udf_RoundNearest (Value,100,0) fn
/* Value Number
1236 1200
6584 6500
9999 9900 */