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

求SQL语句小数后减少到2位
A表

ID   SUM
1     10.0000002
2     10.010
3     15.050
4     18.1999999
...........


要求结果

ID   SUM
1     10.00
2     10.01
3     15.10
4     18.20
...
数据库中不只这四条记录,还有很多,都需要改成保留小数后2位,求教各位大哥美女....

------解决方案--------------------
select id,convert(decimal(9,2),[sum]) from a
------解决方案--------------------

;with cte (id,num) as
(
select 1,10.0000002
union all select 2,10.010
union all select 3,15.050
union all select 4,18.1999999
)
select ID,cast(num as numeric(10,2)) as [sum] from cte

/*
ID sum
1 10.00
2 10.01
3 15.05
4 18.20
*/


------解决方案--------------------
只保留两位小数, 
15.050--->15.10
这个是楼主写错了吧?

------解决方案--------------------

;with cte (id,num) as
(
select 1,10.0000002
union all select 2,10.010
union all select 3,15.050
union all select 4,18.1999999
)

select ID,
       sum=cast(num as decimal(10,2)) 
from cte
/*
ID sum
1 10.0000000
2 10.0100000
3 15.0500000
4 18.1900000
*/

------解决方案--------------------
四舍五入....
------解决方案--------------------
引用:
只保留两位小数, 
15.050--->15.10
这个是楼主写错了吧?


估计是四舍五入了
------解决方案--------------------

create table #test
(id int identity,testPrice money)
go
insert into #test
select 10.0000002
union all select 2100.090
union all select 375.050
union all select 4108.1909999
go
select id,convert(numeric(10,2),testPrice) as testPrice from #test



------解决方案--------------------

create table #test
(id int identity,[SUM] money)
go
insert into #test
select 10.0000002
union all select 2100.090
union all select 375.050
union all select 4108.1909999
go
select id,convert(numeric(10,2),[SUM]) as [SUM] from #test


------解决方案--------------------
引用:
Quote: 引用:

只保留两位小数, 
15.050--->15.10
这个是楼主写错了吧?


估计是四舍五入了