Logo

MODULO

Updated 2023-11-08 20:46:26.263000

Syntax

SELECT [westclintech].[wct].[Modulo] (
  <@Number, float,>
 ,<@Divisor, float,>)

Description

Use the scalar function 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.

Arguments

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

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

Return Type

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.

{"columns":[{"field":"number","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"divisor","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"wct.Modulo","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"built-in function","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"number":"340","divisor":"60","wct.Modulo":"40","built-in function":"40"},{"number":"-340","divisor":"60","wct.Modulo":"20","built-in function":"-40"},{"number":"-340","divisor":"-60","wct.Modulo":"-40","built-in function":"-40"},{"number":"340","divisor":"-60","wct.Modulo":"-20","built-in function":"40"}]}