Login     Register

        Contact Us     Search

XLeratorDB/engineering Online Documentation

SQL Server MOD function


 
Updated: 27 August 2010

Use Modulo to return the amount by which a number exceeds the largest integer multiple of the divisor that is not greater than that number. For positive numbers the remainder and the modulo are the same. They differ for negative numbers. This is the equivalent of the MOD function in EXCEL.
Syntax
SELECT [wctEngineering].[wct].[Modulo] (
   <@Number, float,>
 ,<@Divisor, float,>)
Arguments
@Number
is the number for which you want to find the modulo. @Number is an expression of type float or of a type that can be implicitly converted to float
@Divisor
is the number by which you want to divide @Number. @Divisor is an expression of type float or of a type that can be implicitly converted to float. 
Return Types
float
Remarks
·         Modulo and the built-in modulo function, %, will return different values for negative numbers.
Examples

create
table #m1 (number float, divisor float)
insert into #m1 values (340, 60)
insert into #m1 values (-340, 60)
insert into #m1 values (-340, -60)
insert into #m1 values (340, -60)

select
number
,divisor
,wct.modulo(number, divisor) as [wct.Modulo]
,cast(number as int) % cast(divisor as int) as [built-in function]
from #m1

This produces the following result
number                divisor               wct.Modulo            built-in function
--------------------- --------------------- --------------------- ----------------
 340                   60                    40                    40
-340                   60                    20                   -40
-340                  -60                   -40                   -40
 340                  -60                   -20                    40
 

(4 row(s) affected)



Copyright 2008-2024 Westclintech LLC         Privacy Policy        Terms of Service