简体   繁体   English

在 SQL Server 中截断(非舍入)小数位

[英]Truncate (not round) decimal places in SQL Server

I'm trying to determine the best way to truncate or drop extra decimal places in SQL without rounding.我正在尝试确定在不舍入的情况下截断或删除 SQL 中额外小数位的最佳方法。 For example:例如:

declare @value decimal(18,2)

set @value = 123.456

This will automatically round @value to be 123.46 , which is good in most cases.这会自动轮@value123.46 ,这是在大多数情况下,良好的。 However, for this project, I don't need that.但是,对于这个项目,我不需要那个。 Is there a simple way to truncate the decimals I don't need?有没有一种简单的方法来截断我不需要的小数? I know I can use the left() function and convert back to a decimal.我知道我可以使用left()函数并转换回十进制。 Are there any other ways?还有其他方法吗?

ROUND ( 123.456 , 2 , 1 )

When the third parameter != 0 it truncates rather than rounds当第三个参数!= 0它截断而不是舍入

http://msdn.microsoft.com/en-us/library/ms175003(SQL.90).aspx http://msdn.microsoft.com/en-us/library/ms175003(SQL.90).aspx

Syntax句法

ROUND ( numeric_expression , length [ ,function ] )

Arguments参数

  • numeric_expression Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type. numeric_expression是精确数字或近似数字数据类型类别的表达式,位数据类型除外。

  • length Is the precision to which numeric_expression is to be rounded. length是 numeric_expression 要四舍五入的精度。 length must be an expression of type tinyint, smallint, or int. length 必须是 tinyint、smallint 或 int 类型的表达式。 When length is a positive number, numeric_expression is rounded to the number of decimal positions specified by length.当 length 为正数时,numeric_expression 将四舍五入为 length 指定的小数位数。 When length is a negative number, numeric_expression is rounded on the left side of the decimal point, as specified by length.当长度为负数时,numeric_expression 在小数点左侧四舍五入,如长度所指定。

  • function Is the type of operation to perform. function是要执行的操作类型。 function must be tinyint, smallint, or int.函数必须是 tinyint、smallint 或 int。 When function is omitted or has a value of 0 (default), numeric_expression is rounded.当函数被省略或值为 0(默认)时,numeric_expression 被四舍五入。 When a value other than 0 is specified, numeric_expression is truncated.指定 0 以外的值时,numeric_expression 将被截断。
select round(123.456, 2, 1)
SELECT Cast(Round(123.456,2,1) as decimal(18,2))

实际上,无论第三个参数是 0 或 1 还是 2,它都不会舍入您的值。

CAST(ROUND(10.0055,2,0) AS NUMERIC(10,2))

Here's the way I was able to truncate and not round:这是我能够截断而不是圆的方式:

select 100.0019-(100.0019%.001)

returns 100.0010返回 100.0010

And your example:还有你的例子:

select 123.456-(123.456%.001)

returns 123.450返回 123.450

Now if you want to get rid of the ending zero, simply cast it:现在,如果您想摆脱结尾的零,只需将其强制转换:

select cast((123.456-(123.456%.001)) as decimal (18,2))

returns 123.45返回 123.45

Do you want the decimal or not?你要小数还是不要?

If not, use如果没有,请使用

select ceiling(@value),floor(@value)

If you do it with 0 then do a round:如果你用 0 来做,那么做一个回合:

select round(@value,2)

Round has an optional parameter Round 有一个可选参数

Select round(123.456, 2, 1)  will = 123.45
Select round(123.456, 2, 0)  will = 123.46

Another truncate with no rounding solution and example.另一个没有舍入解决方案和示例的截断。

    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 ROUND(@val,0,1)
SELECT CAST(Value as Decimal(10,2)) FROM TABLE_NAME;

Would give you 2 values after the decimal point.会在小数点后给你 2 个值。 (MS SQL SERVER) (MS SQL 服务器)

Another way is ODBC TRUNCATE function:另一种方法是ODBC TRUNCATE函数:

DECLARE @value DECIMAL(18,3) =123.456;

SELECT @value AS val, {fn TRUNCATE(@value, 2)} AS result

LiveDemo

Output:输出:

╔═════════╦═════════╗
║   val   ║ result  ║
╠═════════╬═════════╣
║ 123,456 ║ 123,450 ║
╚═════════╩═════════╝

Remark:备注:

I recommend using built-in ROUND function with 3rd parameter set to 1.我建议使用内置的ROUND函数,并将第三个参数设置为 1。

I know this is pretty late but I don't see it as an answer and have been using this trick for years.我知道这已经很晚了,但我不认为这是一个答案,并且多年来一直在使用这个技巧。

Simply subtract .005 from your value and use Round(@num,2).只需从您的值中减去 .005 并使用 Round(@num,2)。

Your example:你的例子:

declare @num decimal(9,5) = 123.456

select round(@num-.005,2)

returns 123.45返回 123.45

It will automatically adjust the rounding to the correct value you are looking for.它会自动将舍入调整为您正在寻找的正确值。

By the way, are you recreating the program from the movie Office Space?顺便问一下,您是从电影《办公空间》中重新创建程序吗?

像这样尝试:

SELECT cast(round(123.456,2,1) as decimal(18,2)) 

Please try to use this code for converting 3 decimal values after a point into 2 decimal places:请尝试使用此代码将点后的 3 个十进制值转换为 2 个小数位:

declare @val decimal (8, 2)
select @val = 123.456
select @val =  @val

select @val

The output is 123.46输出为 123.46

I think you want only the decimal value, in this case you can use the following:我认为您只需要十进制值,在这种情况下,您可以使用以下内容:

declare @val decimal (8, 3)
SET @val = 123.456

SELECT @val - ROUND(@val,0,1)

I know this question is really old but nobody used sub-strings to round.我知道这个问题真的很老,但没有人使用子字符串来舍入。 This as advantage the ability to round really long numbers (limit of your string in SQL server which is usually 8000 characters):这是能够舍入非常长的数字的优势(SQL Server 中的字符串限制,通常为 8000 个字符):

SUBSTRING('123.456', 1, CHARINDEX('.', '123.456') + 2)

I think we can go much easier with simpler example solution found in Hackerrank:我认为我们可以通过在 Hackerrank 中找到的更简单的示例解决方案变得更容易:

Problem statement: Query the greatest value of the Northern Latitudes (LAT_N) from STATION that is less than 137.2345.问题陈述:从STATION查询北纬(LAT_N)小于137.2345的最大值。 Truncate your answer to 4 decimal places.将您的答案截断至小数点后 4 位。

SELECT TRUNCATE(MAX(LAT_N),4)
FROM STATION
WHERE LAT_N < 137.23453;

Solution Above gives you idea how to simply make value limited to 4 decimal points.上面的解决方案让您了解如何简单地将值限制为 4 个小数点。 If you want to lower or upper the numbers after decimal, just change 4 to whatever you want.如果您想降低或提高小数点后的数字,只需将 4 更改为您想要的任何值。

ROUND(number, decimals, operation)

number => Required.数字 => 必需。 The number to be rounded要四舍五入的数字
decimals => Required.小数 => 必需。 The number of decimal places to round number to将数字四舍五入到的小数位数
operation => Optional.操作 => 可选。 If 0, it rounds the result to the number of decimal.如果为 0,则将结果四舍五入到小数位数。 If another value than 0, it truncates the result to the number of decimals.如果其他值不是 0,则将结果截断为小数位数。 Default value is 0默认值为 0

SELECT ROUND(235.415, 2, 1)

will give you 235.410会给你235.410

SELECT ROUND(235.415, 0, 1)

will give you 235.000会给你235.000

But now trimming 0 you can use cast但是现在修剪0你可以使用cast

SELECT CAST(ROUND(235.415, 0, 1) AS INT)

will give you 235会给你235

If you desire to take some number like 89.0904987 and turn it into 89.09 by simply omitting the undesired decimal places, simply use the following:如果你89.0904987一些像89.0904987这样的89.09简单地省略89.09不需要的小数位把它变成89.09 ,只需使用以下内容:

select cast(yourColumnName as decimal(18,2))

The following screenshot is from W3Schools SQL Data Types section, which describes what decimal(18,2) is doing:以下屏幕截图来自W3Schools SQL 数据类型部分,它描述了decimal(18,2)正在做什么:

截图来自 https://www.w3schools.com/sql/sql_datatypes.asp

Therefore,所以,

select cast(89.0904987 as decimal(18,2))

gives you: 89.09给你: 89.09

Mod(x,1)是我认为最简单的方法。

select convert(int,@value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM