title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | ms.custom | f1_keywords | helpviewer_keywords | dev_langs | monikerRange | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
% (Modulus) (Transact-SQL) |
The % (modulus) operator returns the remainder of one number divided by another. |
rwestMSFT |
randolphwest |
03/19/2024 |
sql |
t-sql |
reference |
|
|
|
|
>=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric |
[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw-fabricsqldb]
Returns the remainder of one number divided by another.
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions
dividend % divisor
The numeric expression to divide. 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.
The numeric expression by which to divide the dividend. 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.
Determined by data types of the two arguments.
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.
[!INCLUDE article-uses-adventureworks]
The following example divides the number 38
by 5
. The result is 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];
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.
SELECT TOP (100) ProductID,
UnitPrice,
OrderQty,
CAST((UnitPrice) AS INT) % OrderQty AS Modulo
FROM Sales.SalesOrderDetail;
GO
Examples: [!INCLUDE ssazuresynapse-md] and [!INCLUDE ssPDW]
The following example shows results for the %
operator when dividing 3
by 2
.
SELECT TOP(1) 3 % 2
FROM DimEmployee;
[!INCLUDE ssResult]
1