TSQL round to half decimals

2019-05-26 18:26发布

问题:

I want to round to the nearest half decimals (geo coordinates) to do some data visualization. In t-sql, is there a built in function to round to half decimals (if that is the term). Examples of desired result:

    1.1 > 1.0
    1.4 > 1.5
    1.6 > 1.5
    1.9 > 2.0

回答1:

Just multiply by 2, round, and divide by 2.

select round(1.1 * 2, 0) / 2 -- > 1.0
select round(1.4 * 2, 0) / 2 -- > 1.5
select round(1.6 * 2, 0) / 2 -- > 1.5
select round(1.9 * 2, 0) / 2 -- > 2.0

Round on MSDN