我想小数四舍五入SQL Server 2012中,例如:SQL SERVER一轮解释5较低
Select ROUND(1.056,2) -- returns 1.06
Select ROUND(1.055,2) -- returns 1.06
Select ROUND(1.054,2) -- returns 1.05
我怎样才能使第二查询返回1.05四舍五入小数点后第三位,如果5至低?
我想小数四舍五入SQL Server 2012中,例如:SQL SERVER一轮解释5较低
Select ROUND(1.056,2) -- returns 1.06
Select ROUND(1.055,2) -- returns 1.06
Select ROUND(1.054,2) -- returns 1.05
我怎样才能使第二查询返回1.05四舍五入小数点后第三位,如果5至低?
您可以使用此。它会为你工作。
DECLARE @test decimal(10,3) = 1.055
SELECT CASE WHEN round(@test,3,1) - round(@test,2,1) = 0.005 THEN round(@test,2,1) ELSE round(@test,2) END
从字段中只选择2位数字。这将选择第4位或第5位的高位数
select
case when right ('colvalue',1) > right (SUBSTRING('colValue',1,4),1)
then ROUND('colvalue',2)
else ROUND (LEFT ('colvalue',4) ,2) end RoundValue
我仍然希望SELEC T轮(1.056,2,1)返回1.06 – djomla87
您可以使用函数作为round()的第三个参数。如果该函数返回0,则结果会舍入,否则结果将被截断。
-- Syntax for SQL Server and Azure SQL Database
ROUND (numeric_expression , length [ ,function ])
SELECT round(1.055,2,1)
编辑:
下面是一个完整的例子...
create table test
(
n decimal(10,7) not null
)
GO
insert into test (n)
values
(1.050),
(1.051),
(1.052),
(1.053),
(1.054),
(1.055),
(1.056),
(1.057),
(1.058),
(1.059)
GO
select n, round(n,2,iif(n - round(n,2,1)>.005,0,1)) as rounded from test
GO
下面是结果:
n rounded
1.0500000 1.0500000
1.0510000 1.0500000
1.0520000 1.0500000
1.0530000 1.0500000
1.0540000 1.0500000
1.0550000 1.0500000
1.0560000 1.0600000
1.0570000 1.0600000
1.0580000 1.0600000
1.0590000 1.0600000
我会去简单:
DECLARE @test nvarchar(10)
DECLARE @test_string nvarchar(10)
SET @test_string='1.055'
-- SET @test_string='1.056' -- is here to test
SET @test = SUBSTRING(@test_string,5,5)
IF @test=5
BEGIN
SELECT ROUND (LEFT(@test_string,4),2)
END
ELSE
BEGIN
SELECT ROUND (@test_string,2)
END
尝试铸造舍入值:
Select CAST(ROUND(1.056,2) AS NUMERIC(10,2)) -- returns 1.06
Select CAST(ROUND(1.055,2) AS NUMERIC(10,2)) -- returns 1.06
Select CAST(ROUND(1.054,2) AS NUMERIC(10,2)) -- returns 1.05
[SQL Server中向下取整十进制数2008](HTTPS的可能重复:// stackoverflow.com/questions/21072136/rounding-down-decimal-numbers-in-sql-server-2008) –
https://stackoverflow.com/questions/18847374/sql-server-rounding-issue-where-there-is -5 –