Sql server将第5轮解释为更低
我想在sql server 2012中舍入小数,例如:Sql server将第5轮解释为更低,sql,sql-server,rounding,sql-query-store,Sql,Sql Server,Rounding,Sql Query Store,我想在sql server 2012中舍入小数,例如: 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以下,则返回1.05?仅从字段中选择两位数字。这将在第4位或第5位选择高数字 select case when right ( 'colv
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以下,则返回1.05?仅从字段中选择两位数字。这将在第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
仅从字段中选择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
可以使用函数作为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
可以使用函数作为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 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
你可以用这个。这对你有用
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
我会选择简单的:
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
我会选择简单的:
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
尝试强制转换舍入值:
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
可能重复的可能重复的我仍然希望选择轮(1.056,2,1)返回1.06我仍然希望选择轮(1.056,2,1)返回1.06我仍然希望选择轮(1.056,2,1)返回1.06我仍然希望选择轮(1.056,2,1)返回1.06