Modulus operator is used to find the remainder after the division. All programming languages have specific operators to find a remainder like mod, rem, etc. In SQL Server, we can use modulus operator %. However we can also use general approach to find a remainder. Here are two methods
Method 1 : Use modulus operator (%)
declare @n1 int, @n2 int select @n1=16,@n2=7 select @n1%@n2
Method 2 : Use general approach
declare @n1 int, @n2 int select @n1=16,@n2=7 select @n1-@n2*(@n1/@n2)
In both the methods the result is 2
Tags: t-sql, sql_server, sqlserver, tsql, BRH, #TSQL, function, #SQLServer, modulus,
Often overlooked useful application of the modulus operator is extracting a fractional part of the decimal number. For a number Num it will be Num%1.
Thanks Leonid Koyfman. Yes thats simpler way of finding a fractional part