日期:2014-05-17  浏览次数:21386 次

sql以0.5向下取整
sql server 2000中,我想以0.5向下取整,
如 :
0 取整后为 0,
0.0<=@x<0.5 取整后为 0,
0.5<=@x<1  取整后为 0.5,
  1<=@x<1.5  取整后为 1 ,
1.5<=@x<2  取整后为 1.5 ,
  2<=@x<2.5  取整后为 2 ,
2.5<=@x<3  取整后为 2.5 ,
 .......
以此类推。。
怎么实现?望越简洁越好!
之前也有发过“sql以0.5向上取整的帖子”,高手们用 ceiling(@a*2)/2 就解决了,但是现在要向下取整了,不知道咋办了,又来求助各位了!!! 注意是向下哦!!
SQL 0.5 取整 ceiling

------解决方案--------------------
select floor(@a*2)/2
------解决方案--------------------

select floor(0.5*2)/2--0.5

select floor(0.4*2)/2--0


------解决方案--------------------
select ceiling(1.22*2)/2
select floor(1.22*2)/2
------解决方案--------------------

create table a5(x decimal(5,1))

insert into a5(x)
 select 0 union all
 select 0.2 union all
 select 0.6 union all
 select 1.1 union all
 select 1.7 union all
 select 2.3 union all
 select 2.9


select x,
       x-x%0.5 'y'
from a5
 
/*
x                                       y
--------------------------------------- ---------------------------------------
0.0                                     0.0
0.2                                     0.0
0.6                                     0.5
1.1                                     1.0
1.7                                     1.5