Skip to main content

mod

1. MOD Function

The MOD function in H2 database is used to calculate the remainder when one number is divided by another.

2. Syntax

The syntax for the MOD function in H2 database is as follows:

MOD(dividend, divisor)

Arguments

  • dividend: The number to be divided. It should be a numeric value.
  • divisor: The number by which the dividend is divided. It should be a non-zero numeric value.

Return

  • The MOD function returns the remainder when the dividend is divided by the divisor.

3. Notes

  • The MOD function in H2 database returns a value with the same sign as the dividend.
  • If either the dividend or the divisor is NULL, the MOD function will return NULL.
  • Be cautious while using the MOD function with floating-point numbers, as it may result in imprecise remainders due to floating-point arithmetic.

4. Examples

Here are a few examples demonstrating the usage of the MOD function in H2 database:

Example 1 - Calculating the remainder of 10 divided by 3:

SELECT MOD(10, 3) AS remainder;

Output:

remainder
---------
1

Example 2 - Calculating the remainder when dividing a column by a constant:

CREATE TABLE numbers (
id INT PRIMARY KEY,
value INT
);

INSERT INTO numbers VALUES (1, 15), (2, 7), (3, 12);

SELECT id, value, MOD(value, 5) AS remainder
FROM numbers;

Output:

id | value | remainder
---+-------+-----------
1 | 15 | 0
2 | 7 | 2
3 | 12 | 2
  • DIV - Integer division
  • round - Round to a specified number of decimal places
  • CEILING - Round up to the nearest integer
  • floor - Round down to the nearest integer