在SQL Server 2008中舍入到两位数

jha*_*ash 3 t-sql sql-server

它足以通过使用来舍入plsql中的列值round(value,2).例如,1.2222可以通过plsql中的该函数转换为1.22.

我怎么能在SQL Server中这样做?当我使用时round(1.2222,2),它转换为1.2200.我想要1.22.感谢帮助

SWe*_*eko 5

在数学上,1.2200和1.22是完全相同的数字.

数字的显示实际上是一个与数字的物理内容不同的字符串,因此在SQL Server和Oracle中数字的舍入方式相同,只是显示不同.

更多的,关于SQL浮点类型:如果你声明一个变量为具有小号小数,它会始终显示为有小号小数,无论价值,例如

declare @x decimal(10,6)
declare @y decimal(10,2)
set @x =3
set @y=@x -- identical to set @y=cast(@x as decimal(10,2))

select @x -- selects 3.000000
select @y -- selects 3.00

select 'The number is ' + cast(@x as varchar) -- selects 'The number is 3.000000'
select 'The number is ' + cast(@y as varchar) -- selects 'The number is 3.00'
Run Code Online (Sandbox Code Playgroud)

但是,关于3.00和3.0000的含义存在技术差异,这是数字的相对误差.由于所有实数均为四舍五入,因此3.00实际上意味着3 +/- 0.005,而3.0000实际上是3 +/- 0.00005,因为已知更多有效数字.

  • @jhash,问题是列的类型,而不是值.`Cast`更改类型,`round`更改值. (2认同)