case when a='A' then cast(c as decimal(8,3)) 
      when a='B' then cast(c as decimal(8,2)) else null end   as  
为什么当a='A' 时输出来的c 的值是保留3位小数
      当a='B' 时输出来的c 的值还是保留3位小数
这是什么原因

解决方案 »

  1.   

    因为一个字段只能有一种数据类型,此时以第一个when为准
      

  2.   

    case when a='A' then ltrim(cast(c as decimal(8,3))) 
         when a='B' then ltrim(cast(c as decimal(8,2)))
         else null end  as  
      

  3.   

    转成串即可case when a='A' then ltrim(cast(c as decimal(8,3))) 
        when a='B' then ltrim(cast(c as decimal(8,2))) 
        else null end  as  
      

  4.   

    case when a='A' then rtrim(cast(c as decimal(8,3)))
          when a='B' then rtrim(cast(c as decimal(8,2))) end  as [列]--else null 可不用定義
      

  5.   


    declare @tb table
    (
    name nvarchar(50),
    value decimal(10,2)
    )insert into @tb values('A',10.125)
    insert into @tb values('B',10.124)select 
    case when name='A' then cast(value as decimal(8,3)) 
         when name='B' then cast(value as decimal(8,2))
     else null end VALUE
    from @tb

    结果:
    10.130
    10.120试验了一下,在case中结果以最小的位数四舍五入,然后补零至最大位数
    例子中就是说其实结果是decimal(8,2)两位,然后以decimal(8,3)再补一位0