首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求关于小数点进位的函数解决思路

2012-08-11 
求关于小数点进位的函数要求将数值中小数点第三位只要不为0 就要进位并且保留2位小数点SQL codecreate tab

求关于小数点进位的函数
要求将数值中小数点第三位只要不为0 就要进位并且保留2位小数点

SQL code
create table #abc(num decimal(14,6) null)insert into #abc(num)select 123.211432unionselect 123.219103unionselect 123.210921


要求结果:
123.22
123.22
123.21

[解决办法]
SQL code
create table #abc(num decimal(14,6) null)insert into #abc(num)select 123.211432unionselect 123.219103unionselect 123.210921select ceiling(cast(LEFT(LTRIM(num),charindex('.',ltrim(num))+3) as decimal(14,3))*100)/100as numfrom #abc/*num-------------------123.210000123.220000123.220000*/
[解决办法]
SQL code
create table #abc(num decimal(14,6) null)insert into #abc(num)select 123.211432unionselect 123.219103unionselect 123.210921select    ceiling(cast(floor(num*1000)/10.0 as float))/100from #abc/*(无列名)123.21123.22123.22*/
[解决办法]
create table #abc
(num decimal(14,6) null)

insert into #abc
(num)
select 123.211432
union
select 123.219103
union
select 123.210921

select * , case when '0' = substring( cast ( num as varchar) , charindex ( '.' ,cast ( num as varchar) ) +3 ,1)
then cast ( num as decimal (12,2 ) ) else cast( ceiling (num * 100)/100. as decimal(12,2) ) end

from #abc

热点排行
Bad Request.