Click to Rate and Give Feedback
TechNet
TechNet Library
SQL Server
SQL Server 2008
Database Engine
Technical Reference
 % (Modulo) (Transact-SQL)

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (0)
Other versions are also available for the following:
SQL Server 2008 Books Online (June 2009)
% (Modulo) (Transact-SQL)

Returns the remainder of one number divided by another.

Topic link icon Transact-SQL Syntax Conventions

dividend % divisor
dividend

Is 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.

divisor

Is 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.

The modulo arithmetic operator can be used 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.

Determined by data types of the two arguments.

A. Simple example

The following example divides the number 38 by 5. 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. Example using columns in a table

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.

USE AdventureWorks;
GO
SELECT TOP(100)ProductID, UnitPrice, OrderQty,
   CAST((UnitPrice) AS int) % OrderQty AS Modulo
FROM Sales.SalesOrderDetail;
GO
Tags What's this?: math (x) mod (x) Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker