我正在尝试确定在SQL中截断或删除额外的小数位而不舍入的最佳方法。例如:
declare @value decimal(18,2)
set @value = 123.456
这将自动将@value舍入为123.46,这在大多数情况下都很好。然而,对于这个项目,我不需要那样。有没有一种简单的方法来截断我不需要的小数?我知道我可以使用left()函数并将其转换回小数。还有别的办法吗?
我正在尝试确定在SQL中截断或删除额外的小数位而不舍入的最佳方法。例如:
declare @value decimal(18,2)
set @value = 123.456
这将自动将@value舍入为123.46,这在大多数情况下都很好。然而,对于这个项目,我不需要那样。有没有一种简单的方法来截断我不需要的小数?我知道我可以使用left()函数并将其转换回小数。还有别的办法吗?
你到底要不要小数点?
如果没有,请使用
select ceiling(@value),floor(@value)
如果你用0做,那么做一轮:
select round(@value,2)
ROUND ( 123.456 , 2 , 1 )
当第三个参数!= 0时,它截断而不是舍入。
语法
ROUND ( numeric_expression , length [ ,function ] )
参数
numeric_expression Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type. length Is the precision to which numeric_expression is to be rounded. length must be an expression of type tinyint, smallint, or int. When length is a positive number, numeric_expression is rounded to the number of decimal positions specified by length. When length is a negative number, numeric_expression is rounded on the left side of the decimal point, as specified by length. function Is the type of operation to perform. function must be tinyint, smallint, or int. When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.
以下是我能够截断而不是舍入的方法:
select 100.0019-(100.0019%.001)
返回100.0010
你的例子是:
select 123.456-(123.456%.001)
返回123.450
现在,如果你想去掉0结尾,只需对它进行强制转换:
select cast((123.456-(123.456%.001)) as decimal (18,2))
返回123.45
另一个没有舍入解决方案的截断和示例。
Convert 71.950005666 to a single decimal place number (71.9)
1) 71.950005666 * 10.0 = 719.50005666
2) Floor(719.50005666) = 719.0
3) 719.0 / 10.0 = 71.9
select Floor(71.950005666 * 10.0) / 10.0
请尝试使用此代码将一个点后的3个十进制值转换为2位小数:
declare @val decimal (8, 2)
select @val = 123.456
select @val = @val
select @val
输出值为123.46
Round有一个可选参数
Select round(123.456, 2, 1) will = 123.45
Select round(123.456, 2, 0) will = 123.46
我认为你只需要小数, 在这种情况下,你可以使用以下方法:
declare @val decimal (8, 3)
SET @val = 123.456
SELECT @val - ROUND(@val,0,1)
另一种方法是ODBC的TRUNCATE函数:
DECLARE @value DECIMAL(18,3) =123.456;
SELECT @value AS val, {fn TRUNCATE(@value, 2)} AS result
现场演示
输出:
╔═════════╦═════════╗
║ val ║ result ║
╠═════════╬═════════╣
║ 123,456 ║ 123,450 ║
╚═════════╩═════════╝
备注:
我建议使用内置的ROUND函数,第3个参数设置为1。
我知道这有点晚了,但我不认为这是一个答案,我已经使用这个技巧很多年了。
只需从值中减去0.005并使用Round(@num,2)。
你的例子:
declare @num decimal(9,5) = 123.456
select round(@num-.005,2)
返回123.45
它会自动调整四舍五入到您正在寻找的正确值。
顺便问一下,你是在重现电影《办公空间》里的程序吗?
我知道这个问题非常古老,但没有人使用子字符串舍入。这是对很长的数字进行四舍五入的优势(SQL server中字符串的限制通常是8000个字符):
SUBSTRING('123.456', 1, CHARINDEX('.', '123.456') + 2)
我认为我们可以在Hackerrank中找到更简单的示例解决方案:
问题说明:查询北纬的最大值 (LAT_N)来自小于137.2345的STATION。截断你的答案 小数点后4位。
SELECT TRUNCATE(MAX(LAT_N),4)
FROM STATION
WHERE LAT_N < 137.23453;
上面的解决方案告诉你如何简单地将值限制在4个小数点内。如果你想降低或提高小数后面的数字,只要把4改成你想要的任何数字。
ROUND(number, decimals, operation)
number =>必选参数。要四舍五入的数字 decimals =>必选。要四舍五入的小数位数 operation =>可选。如果是0,则将结果四舍五入为小数。如果其他值大于0,则将结果截断为小数的个数。默认值为0
SELECT ROUND(235.415, 2, 1)
给你235.410
SELECT ROUND(235.415, 0, 1)
给你23.5万
但是现在你可以使用cast
SELECT CAST(ROUND(235.415, 0, 1) AS INT)
会得到235
如果你想要取一些像89.0904987这样的数字,并通过省略不需要的小数位将其转换为89.09,只需使用以下方法:
select cast(yourColumnName as decimal(18,2))
下面的截图来自W3Schools SQL数据类型部分,它描述了decimal(18,2)的作用:
因此,
select cast(89.0904987 as decimal(18,2))
给你:89.09