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位小数
这是什么原因
when a='B' then cast(c as decimal(8,2)) else null end as
为什么当a='A' 时输出来的c 的值是保留3位小数
当a='B' 时输出来的c 的值还是保留3位小数
这是什么原因
when a='B' then ltrim(cast(c as decimal(8,2)))
else null end as
when a='B' then ltrim(cast(c as decimal(8,2)))
else null end as
when a='B' then rtrim(cast(c as decimal(8,2))) end as [列]--else null 可不用定義
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