SQL Server舍入功能概述– SQL舍入,上限和下限

Developers deal with numerous data types on a day- to-day basis. We need to change the data type or format as per the user requirement. We use ‘SQL Server rounding function’ like SQL Round, Ceiling and Floor to round the values to the nearest numbers. We perform an arithmetic calculation on data as well. It is a challenging task to change the value of a number to an approximate number. We do not want to display decimal numbers in the application front end.

开发人员每天处理大量数据类型。 我们需要根据用户要求更改数据类型或格式。 我们使用“ SQL Server舍入函数”(例如SQL Round,Ceiling和Floor)将值舍入到最接近的数字。 我们还对数据执行算术计算。 将数字的值更改为近似数字是一项艰巨的任务。 我们不想在应用程序前端显示十进制数字。

The output of the aforementioned round functions depends upon the data types as well. Let’s have a look at each SQL Server Rounding functions definitions in this article.

上述回合函数的输出也取决于数据类型。 让我们看一下本文中的每个SQL Server舍入函数定义。

SQL Server舍入函数– Round() (SQL Server Rounding function – Round())

In SQL Server, Round function round a number to a specified length or precision.

在SQL Server中,舍入函数将数字舍入到指定的长度或精度。

The SQL Round function accepts three parameters as per the following syntax: ROUND ( numeric_expression , length [ ,function ] )

SQL Round函数按照以下语法接受三个参数: ROUND(numeric_expression,length [,function])

  • Numeric_expression: It is an exact number or numeric data type expression. We cannot use a bit of data type in this parameter Numeric_expression :它是确切的数字或数字数据类型表达式。 我们不能在此参数中使用一点数据类型
  • Length: It is the number of decimal places to which we want to round the number. We can use both positive and negative values in this. We can use only 长度:这是我们要将数字四舍五入到的小数位数。 我们可以在此同时使用正值和负值。 我们只能在此参数中使用tinyint, tinyintsmallint, or smallintint data types in this parameter int数据类型
  • Function: It is an optional parameter and specifies the truncation point of the value. The default value for this parameter is zero. If you do not specify any values (default value), it rounds the numeric_expression. If the value is other than zero, it truncates the numeric_expression 功能:这是一个可选参数,用于指定值的截断点。 此参数的默认值为零。 如果未指定任何值(默认值),则将舍入numeric_expression。 如果该值不是零,它将截断numeric_expression

SQL Server舍入功能– CEILING() (SQL Server Rounding function – CEILING())

We use the SQL CEILING function to evaluate the value and return the smallest integer greater than, or equal to, the specified numeric expression. It only accepts one value.

我们使用SQL CEILING函数计算值,并返回大于或等于指定数字表达式的最小整数。 它仅接受一个值。

Syntax of SQL CEILING function: CEILING ( numeric_expression )

SQL CEILING函数的语法: CEILING(numeric_expression)

  • Numeric_expression: It is an exact number or numeric data type expression. We cannot use a bit of data type in this parameter Numeric_expression :它是确切的数字或数字数据类型表达式。 我们不能在此参数中使用一点数据类型

SQL Server舍入函数– FLOOR() (SQL Server Rounding function – FLOOR())

The SQL Floor function is similar to a CEILING function with one difference. It returns the largest smallest integer greater than, or equal to, the specified numeric expression. It also accepts one value.

SQL Floor函数类似于CEILING函数,但有一个区别。 它返回大于或等于指定数字表达式的最大最小整数。 它还接受一个值。

Syntax of SQL FLOOR function: FLOOR ( numeric_expression )

SQL FLOOR函数的语法: FLOOR(numeric_expression)

  • Numeric_expression: It is an exact number or numeric data type expression. We cannot use a bit of data type in this parameter Numeric_expression :它是确切的数字或数字数据类型表达式。 我们不能在此参数中使用一点数据类型

Let’s walk through SQL Server Rounding functions with examples in the next section.

让我们在下一部分中通过示例逐步介绍SQL Server舍入功能。

示例1:具有整数数据类型SQL Server舍入函数 (Example 1: SQL Server Rounding functions with Integer data type)

In this example, we define a variable to hold integer value and use SQL Server Rounding functions to view the output.

在此示例中,我们定义了一个变量来保存整数值,并使用SQL Server舍入函数查看输出。

正整数值 (Positive Integer value)
DECLARE @value INT;
SET @value = 10;
 
SELECT ROUND(@value, 0);
 
SELECT CEILING(@value);  
 
SELECT FLOOR(@value);

In the output, we can see that all three SQL Rounding functions (Round, CEILING and Floor) return the same output for the positive integer value.

在输出中,我们可以看到所有三个SQL Rounding函数(Round,CEILING和Floor)都为正整数值返回相同的输出。

We do not have any decimal digit; therefore, Round function does not round the value. Similarly, CEILING and FLOOR function also do not return the smallest or largest integer value.

我们没有任何十进制数字; 因此,舍入函数不会舍入该值。 同样,CEILING和FLOOR函数也不返回最小或最大整数值。

SQL Server rounding functions - SQL Round, SQL Ceiling, SQL Floor
负整数值 (Negative Integer value)

Let’s use negative integer value in the previous example and see the difference in the output.

让我们在上一个示例中使用负整数值,并查看输出的差异。

DECLARE @value INT;
SET @value = -10;
SELECT ROUND(@value, 2); 
SELECT CEILING(@value);   
SELECT FLOOR(@value);

We also get similar output for the negative integer value as well.

对于负整数值,我们也得到类似的输出。

SQL Server rounding function with negative integer
长度为正或负的正整数值 (Positive Integer Value with Positive or Negative Length)

In the round function, we can use both positive and negative numbers for the second parameter Length. Let’s use different values in the Length parameter for the round function.

在舍入函数中,我们可以对第二个参数Length使用正数和负数 让我们在Round函数的Length参数中使用不同的值。

SELECT ROUND(8, 0); 
SELECT ROUND(8, 1); 
SELECT ROUND(8, 2); 
SELECT ROUND(8, -1); 
SELECT ROUND(8, -2);
SELECT ROUND(8, -3);

In the output, we can see the following.

在输出中,我们可以看到以下内容。

  • Positive length, it always returns the same number without any rounding 长度的正整数,它总是返回相同的数字而不进行舍入
  • Negative length, it rounds the number to the nearest tens place 长度的正整数,它将数字四舍五入到最接近的十位数
  • Negative length but length is larger than the number of digits before the decimal point, ROUND always returns 0 但长度大于小数点前的位数的正整数,ROUND始终返回0

Positive Integer Value with Positive or Negative Length

Let’s look at another example of three digit positive integer number. It rounds the value until negative length 3 to the nearest tens place. If we use negative length 4 with a three-digit number, it returns 0.

让我们看另一个三位数正整数的例子。 它将值四舍五入,直到负数长度3到最接近的十位数。 如果我们使用负数长度4和三位数,则返回0。

SELECT ROUND(888, 0); 
SELECT ROUND(888, 1); 
SELECT ROUND(888, 2); 
SELECT ROUND(888, -1); 
SELECT ROUND(888, -2);
SELECT ROUND(888, -3);

Positive Integer Value with  Negative Length

Let’s use another example with integer value 457.

我们再来看一个整数值为457的示例。

SELECT ROUND(457, -1); 
SELECT ROUND(457, -2);
SELECT ROUND(457, -3); 
SELECT ROUND(457, -4);

In this example, we can note the following regards to nearest tens place.

在此示例中,我们可以注意以下有关十位数的注意事项。

  • SELECT ROUND(457, -1) rounds down the value to 460

    SELECT ROUND(457,-1)将值四舍五入到460
  • SELECT ROUND(457, -2); rounds up the value to 500

    SELECT ROUND(457,-2); 将值四舍五入到500

Negative Integer Value with Negative Length
负整数的负整数值 (Negative Integer Value with Negative Length)

In this example, let’s use a negative length integer value along with the negative length as well. It also rounds up and down the value as per the length.

在此示例中,我们还将负长度整数值与负长度一起使用。 它还根据长度对值进行四舍五入。

SELECT ROUND(-888, -1); 
SELECT ROUND(-888, -2);
SELECT ROUND(-888, -3); 
SELECT ROUND(-888, -4);

Negative Integer Value with Negative Length

示例2:具有十进制数据类型SQL四舍五入函数 (Example 2: SQL Rounding functions with decimal data type)

In this example, let’s use a variable with decimal data type and check the output for different length precision.

在此示例中,让我们使用具有十进制数据类型的变量,并检查输出的不同长度精度。

长度为正的十进制数据类型值 (Decimal data type value with positive Length)

In the following query, use the positive length for the decimal data type.

在以下查询中,对十进制数据类型使用正长度。

DECLARE @value DECIMAL(10, 2);
SET @value = 12.07;
SELECT ROUND(@value, 1);  
SELECT ROUND(@value, 2);
SELECT ROUND(@value, 3);

In this example, we can see that with decimal values round up to the nearest value as per the length.

在此示例中,我们可以看到十进制值根据长度四舍五入到最接近的值。

Decimal data type value with positive Length
负长度的十进制数据类型值 (Decimal data type value with negative Length)

In the following query, use the negative length for the decimal data type.

在以下查询中,对十进制数据类型使用负长度。

DECLARE @value DECIMAL(10, 2);
SET @value = 12.07;
SELECT ROUND(@value, -1)
SELECT ROUND(@value, -2) 
SELECT ROUND(@value, -3)

We can see that the output is rounded to nearest value .If length is insufficient, it also return zero value.

我们可以看到输出被四舍五入到最接近的值,如果长度不足,它也会返回零值。

Decimal data type value with negative Length
使用CEILING和Floor SQL Server舍入函数的十进制数据类型值 (Decimal data type value with CEILING and Floor SQL Server rounding functions)
DECLARE @value DECIMAL(10, 2);
SET @value = 12.07;
SELECT CEILING(@value)   
SELECT FLOOR(@value)

Decimal data type value with SQL CEILING and SQL Floor rounding functions

示例3:具有float数据类型SQL四舍五入函数 (Example 3: SQL Rounding functions with float data type)

具有正负长度的浮点数据类型值 (Float data type value with positive and negative Length)

Let’s use the float data type with positive and negative length values.

让我们将float数据类型与正负长度值一起使用。

DECLARE @value float(10)
SET @value = 11.23456
SELECT ROUND(@value, -1)
SELECT ROUND(@value, -2) 
SELECT ROUND(@value, 1);  
SELECT ROUND(@value, 2);
SELECT ROUND(@value, 3);
SELECT ROUND(@value, 4);

In this example, you can see the following things.

在此示例中,您可以看到以下内容。

  • For Positive length 1, we get the output 11.2 because the next digit is 3 that is less than 5

    对于正数长度1,我们得到输出11.2,因为下一位数字是3,小于5
  • For Positive length 2, we get the output 11.23 because the next digit is 4 that is less than 5

    对于正数长度2,我们得到输出11.23,因为下一位数字是4,小于5
  • For Positive length 3, we get the output 11.235 because the next digit is 5 that is equals to 5

    对于正长度3,我们得到输出11.235,因为下一位数字是5等于5
  • For Positive length 4, we get the output 11.2346 because the next digit is 6 that is greater than 5

    对于正数长度4,我们得到输出11.2346,因为下一个数字是6大于5

Float data type value with positive and negative Length

We can also understand using the following example.

我们还可以使用以下示例来理解。

DECLARE @value float(10)
SET @value = .92719
SELECT ROUND(@value, 1);  
SELECT ROUND(@value, 2);
SELECT ROUND(@value, 3);
SELECT ROUND(@value, 4);
  • For Positive length 1, we get the output 0.9 because the next digit is 2 that is less than 5

    对于正数长度1,我们得到输出0.9,因为下一位数字是2,小于5
  • For Positive length 2, we get the output 0.93 because the next digit is 7 that is greater than 5

    对于正长度2,我们得到输出0.93,因为下一位数字是7,大于5
  • For Positive length 3, we get the output 0.927 because the next digit is 1 that that is less than 5

    对于正数长度3,我们得到输出0.927,因为下一位数字是小于5的1
  • For Positive length 4, we get the output 0.9272 because the next digit is 9 that is greater than 5

    对于正数长度4,我们得到输出0.9272,因为下一位数字是9大于5

Float data type value with positive and negative Length SQL Rounding functions
具有CEILING和Floor SQL Server舍入功能的浮点数据类型值 (Float data type value with CEILING and Floor SQL Server rounding functions)
DECLARE @value float(10);
SET @value = 0.92719;
SELECT CEILING(@value)   
SELECT FLOOR(@value)

Float data type value with SQL CEILING and SQL Floor rounding functions

示例4:具有数值数据类型SQL舍入函数 (Example 4: SQL Rounding functions with Numeric data type)

具有正负长度的数值数据类型值 (Numeric data type value with positive and negative Length)

In this example, we will use a numeric data type with both the positive and negative length values. It follows the same behavior as of decimal data type.

在此示例中,我们将使用具有正负长度值的数字数据类型。 它遵循与十进制数据类型相同的行为。

DECLARE @value numeric(10,5)
SET @value = .92831
SELECT ROUND(@value, 1);  
SELECT ROUND(@value, 2);
SELECT ROUND(@value, 3);
SELECT ROUND(@value, 4);
SELECT ROUND(@value, -1);  
SELECT ROUND(@value, -2);
  • For Positive length 1, we get the output 0.90000 because the next digit is 2 that is less than 5

    对于正数长度1,我们得到的输出为0.90000,因为下一位数字是2,小于5
  • For Positive length 2, we get the output 0.93000 because the next digit is 8 that is greater than 5

    对于正长度2,我们得到的输出为0.93000,因为下一位数字是8,大于5
  • For Positive length 3, we get the output 0.92800 because the next digit is 3 that is less than 5

    对于正数长度3,我们得到输出0.92800,因为下一位数字是3,小于5
  • For Positive length 4, we get the output 0.92830 because the next digit is 1 that is less than 5

    对于正数长度4,我们得到输出0.92830,因为下一位数字是小于5的1
  • For negative length, we get the output 0.00000

    对于负长度,我们得到输出0.00000

    Numeric data type value with positive and negative Length

具有CEILING和Floor SQL Server舍入功能的数值数据类型值 (Numeric data type value with CEILING and Floor SQL Server rounding functions)
DECLARE @value numeric(10,5)
SET @value = .92831
SELECT CEILING(@value)   
SELECT FLOOR(@value)

Numeric data type value with CEILING and Floor rounding functions

示例5:使用第三个参数SQL Server舍入函数截断 (Example 5: SQL Server rounding function Truncation using the third argument)

In the above examples, we have not used the third argument Function to truncate the result or round the result. By default, it uses value 0 to round the result. If we want to truncate the result, we can specify a value other than 0.

在上面的示例中,我们没有使用第三个参数Function来截断结果或舍入结果。 默认情况下,它使用值0舍入结果。 如果要截断结果,可以指定非0的值。

SELECT 
  ROUND(7.4567, 2) 'Rounded (by default)',
  ROUND(7.4567, 2, 0) 'Rounded  with default function value',
  ROUND(7.4567, 2, 1) 'Rounded  with explicit function value'

SQL Server rounding function Truncation using a third argument

结论 (Conclusion)

In this article, we explored the SQL Server Rounding functions with different data types. I would suggest reviewing them as per your environment. If you have any comments or questions, feel free to leave them in the comments below.

在本文中,我们探讨了具有不同数据类型SQL Server舍入函数。 我建议根据您的环境对其进行审查。 如果您有任何意见或疑问,请随时将其留在下面的评论中。

翻译自: https://www.sqlshack.com/overview-of-sql-server-rounding-functions-sql-round-ceiling-and-floor/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值