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%

解决方案 »

  1.   

    create table #tmp(b decimal(18,4) ) insert into #tmp 
    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 
      

  2.   


    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 行受影响)
      

  3.   

    create table #tmp(b decimal(18,4) ) insert into #tmp 
    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 行受影响)
    */
      

  4.   


    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%
      

  5.   


    把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 
      

  6.   

    7楼写错了,楼主见谅,改正一下
    select rtrim(CONVERT(DECIMAL(12,2), b*100)) +'%' from #tmp
    ------------------------------------------
    3.00%
    25.32%
    53.00%(3 row(s) affected)
      

  7.   

    去掉末尾0
    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)
      

  8.   

    select cast(cast(b*100 as numeric(10,2) as varchar) +'%' from #tmp