create table #tmp(b decimal(18,4) )insert into #tmp
select 0.2532
union
select 0.53
union
select 0.03
select cast(b*100 as varchar) +'%' from #tmp
drop table #tmp
输出如下:
3.0000%
25.3200%
53.0000%
但我希望是
3.00%
25.32%
53.00%
select 0.2532
union
select 0.53
union
select 0.03
select cast(b*100 as varchar) +'%' from #tmp
drop table #tmp
输出如下:
3.0000%
25.3200%
53.0000%
但我希望是
3.00%
25.32%
53.00%
select 0.2532
union
select 0.53
union
select 0.03
select cast(cast(b*100 as decimal(10,2)) as varchar)+'%' from #tmp
drop table #tmp
create table #tmp(b decimal(18,4) )insert into #tmp
select 0.2532
union
select 0.53
union
select 0.03
select cast(CONVERT(DECIMAL(12,2), b*100) as varchar) +'%' from #tmp
drop table #tmp -------------------------------
3.00%
25.32%
53.00%(3 行受影响)
select 0.2532
union
select 0.53
union
select 0.03 select cast(cast(b*100. as decimal(18,2)) as varchar) +'%' from #tmp
drop table #tmp /*-------------------------------
3.00%
25.32%
53.00%(3 行受影响)
*/
create table #tmp(b decimal(18,4) )insert into #tmp
select 0.2532
union
select 0.53
union
select 0.03
select cast(CONVERT(DECIMAL(12,2), b*100) as varchar) +'%' from #tmp
drop table #tmp -------------------------------
3.00%
25.32%
53.00%
把decimal(18,4)改成decimal(18,2)) create table #tmp(b decimal(18,2) ) insert into #tmp
select 0.2532
union
select 0.53
union
select 0.03
select cast(b*100 as varchar) +'%' from #tmp
select rtrim(CONVERT(DECIMAL(12,2), b*100)) +'%' from #tmp
------------------------------------------
3.00%
25.32%
53.00%(3 row(s) affected)
create table #tmp(b decimal(18,4) ) insert into #tmp
select 0.2532 union
select 0.2550 union
select 0.53 union
select 0.03 select b = case when ceiling(b*100) = b*100 then rtrim(ceiling(b*100))+'%'
when right(convert(decimal(18,2),b*100),1) = '0' then stuff(convert(decimal(18,2),b*100),len(convert(decimal(18,2),b*100)),1,'')+'%'
else rtrim(convert(decimal(18,2),b*100))+'%'
end
from #tmp
b
---------
3%
25.32%
25.5%
53%(4 row(s) affected)