mod()

Returns the remainder of an integer division.

Syntax

mod(NUM_EXPR, DIVISOR)

Parameters

NUM_EXPR
The dividend of the division. Must be a 32-bit integer or 64-bit integer type.
DIVISOR
The divisor. Must be a 32-bit integer or 64-bit integer type.

Description

The mod() function returns the remainder of dividing NUM_EXPR by DIVISOR as a 64-bit integer.

Returns null in the following cases:

  • NUM_EXPR or DIVISOR is null.
  • NUM_EXPR or DIVISOR is not an integer type (32-bit integer or 64-bit integer).
  • DIVISOR is 0 (division by zero is prevented).

Error codes

Error codeDescription
invalid-mod-argsThe number of arguments is not exactly 2.

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Calculate the remainder after removing the hundreds digit from the HTTP status code.

    table limit=5 WEB_APACHE_SAMPLE | eval result = mod(status, 100)
    | fields status, result
    
  2. Dividing by zero returns null.

    json "{}" | eval r = mod(5, 0)
    | # r: null
    
  3. NULL input

    json "{}" | eval r = mod(null, 3)
    | # r: null
    

Compatibility

The mod() function is available since before Sonar 4.0.