我正在尝试确定在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()函数并将其转换回小数。还有别的办法吗?
当前回答
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.
其他回答
另一个没有舍入解决方案的截断和示例。
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
试着这样做:
SELECT cast(round(123.456,2,1) as decimal(18,2))
这将删除任何数字的小数部分
SELECT ROUND(@val,0,1)
SELECT CAST(Value as Decimal(10,2)) FROM TABLE_NAME;
小数点后会有两个值。(mssql server)
SELECT Cast(Round(123.456,2,1) as decimal(18,2))