Modulo operator provides the remainder of one number divided by another number.
Syntax of % ( Modulo ) Operators :
dividend % divisor
Dividend is the numeric expression to divide. Dividend must be any expression of integer data type in sql server.
Divisor is the numeric expression to divide the dividend. Divisor must be expression of integer data type except in sql server.
Example of % ( Modulo ) Operator :
Example 1 : Displays use of modulo for numeric numbers.
SELECT 15%2
Output
1
Above example explains how can we use modulo operator to get remainder of numeric numbers.
Example 2 : Example of performing modulo operation on table field UnitPrice.
SELECT ProductName, UnitPrice % 4 AS Price
FROM Products
Output
ProductName Price
Chai 2.00
Chang 3.00
Aniseed Syrup 1.35
Chef Anton's Cajun Seasoning 1.00
Chef Anton's Gumbo Mix 2.70
Above example displays how can we perform modulo operation on table field UnitPrice by numeric value.
Example 3 : Example of modulo operator between 2 fields of tables.
SELECT ProductName, UnitPrice % Discount AS Remainder
FROM Products
Output
ProductName Remaindert
Chai 3
Chang 5
Aniseed Syrup 2
Chef Anton's Cajun Seasoning 4
Chef Anton's Gumbo Mix 8
Above example displays remainder by performing % operation between UnitPrice and Discount table fields.