%(取模)(Transact-SQL)% (Modulus) (Transact-SQL)

适用对象: 是SQL Server 是Azure SQL 数据库 是Azure Synapse Analytics (SQL DW) 是并行数据仓库 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

返回两数相除后的余数。Returns the remainder of one number divided by another.

主题链接图标 TRANSACT-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

dividend % divisor  

参数Arguments

被除数dividend
被除数的数值表达式。Is the numeric expression to divide. dividend 必须为整数和货币数据类型类别中任意一种数据类型的有效表达式,或者为 numeric 数据类型。dividend must be a valid expression of any one of the data types in the integer and monetary data type categories, or the numeric data type.

除数divisor
除数的数值表达式。Is the numeric expression by which to divide the dividend. divisor 必须为整数和货币数据类型类别中任意一种数据类型的任意有效表达式,或者为 numeric 数据类型 。divisor must be any valid expression of any one of the data types in the integer and monetary data type categories, or the numeric data type.

结果类型Result Types

由两个参数的数据类型确定。Determined by data types of the two arguments.

RemarksRemarks

取模算术运算符可以和列名、数值常量或任何具有整数和货币数据类型类别或 numeric 数据类型的有效表达式组合一起用于 SELECT 语句的选择列表中 。You can use the modulo arithmetic operator in the select list of the SELECT statement with any combination of column names, numeric constants, or any valid expression of the integer and monetary data type categories or the numeric data type.

示例Examples

A.A. 简单示例Simple example

下面的示例用 38 除以数字 5。The following example divides the number 38 by 5. 所得结果的整数部分为 7。该示例演示了取模运算如何返回余数 3。This results in 7 as the integer portion of the result and demonstrates how modulo returns the remainder of 3.

SELECT 38 / 5 AS Integer, 38 % 5 AS Remainder ;  
  

B.B. 使用表中的列的示例Example using columns in a table

以下示例返回产品 ID 号、产品单价、除以每种产品的单价后得到的模(余数)、转换为整数值,以及订购的产品数。The following example returns the product ID number, the unit price of the product, and the modulo (remainder) of dividing the price of each product, converted to an integer value, into the number of products ordered.

-- Uses AdventureWorks  
  
SELECT TOP(100)ProductID, UnitPrice, OrderQty,  
   CAST((UnitPrice) AS int) % OrderQty AS Modulo  
FROM Sales.SalesOrderDetail;  
GO  

示例:Azure Synapse Analytics (SQL DW)Azure Synapse Analytics (SQL DW)并行数据仓库Parallel Data WarehouseExamples: Azure Synapse Analytics (SQL DW)Azure Synapse Analytics (SQL DW) and 并行数据仓库Parallel Data Warehouse

C:简单示例C: Simple example

以下示例显示将 3 除以 2 时运算符 % 的结果。The following example shows results for the % operator when dividing 3 by 2.

-- Uses AdventureWorks  
  
SELECT TOP(1) 3%2 FROM dimEmployee;  

下面是结果集:Here is the result set.

---------   
1         

另请参阅See Also

内置函数 (Transact-SQL) Built-in Functions (Transact-SQL)
LIKE (Transact-SQL) LIKE (Transact-SQL)
运算符 (Transact-SQL) Operators (Transact-SQL)
SELECT (Transact-SQL) SELECT (Transact-SQL)
%=(取模赋值)(Transact-SQL) %= (Modulus Assignment) (Transact-SQL)
复合运算符 (Transact-SQL)Compound Operators (Transact-SQL)