执行这些并检查结果为什么会这样?
declare @a decimal(8,3) =235.363
declare @b int =1
select case @b
when 1 then cast(@a as int)
when 2 then CAST(@a as decimal(8,3))
end
Result : 235.000
<小时/>
declare @a decimal(8,3) =235.363
declare @b int =1
select case @b
when 1 then cast(@a as int)
--when 2 then CAST(@a as decimal(8,3))
end
Result : 235
<小时/>
declare @a decimal(8,3) =235.363
declare @b int =1
select case @b
when 1 then cast(@a as tinyint)
when 2 then CAST(@a as float)
end
Result : 235
最佳答案
所见并非所得。
对于列type,SQL Server 选择正确的、更宽的类型(float
over tinyint
、decimal
超过int
)。您可以通过执行 select into
来验证这一点而不仅仅是选择。
只是显示规则不同。
当所选列类型为 float
时,如果没有小数部分,您将看不到尾随的 .000
。
对于设置了显式位置的 decimal
,例如 decimal(8,3)
,即使没有小数部分,您也会看到尾随的 .000
。如果删除说明符并仅保留 decimal
作为列类型,.000
将消失。
所有这些都不会影响实际的列类型,它始终是最宽的类型。
关于sql - 转换为 Integer 无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8543224/