MOD

Return the remainder of dividing one number by another (truncated division, sign follows dividend).

Category: mathReturns: NUMERICDialect: Standard

Syntax

MOD(dividend, divisor)

Description

## Overview Returns the remainder of dividing the dividend by the divisor using truncated division: the quotient is truncated toward zero, and the remainder takes the sign of the dividend. This is the convention used by the C / and % operators and by most SQL engines that expose MOD directly. When the non-negative remainder is required (for example, when the output is used as an array or partition index), use PMOD or ((x % d) + d) % d instead. ## Behavior - Accepts any numeric type for both arguments. - Returns a value of the promoted common type. - Returns NULL if either argument is NULL. - Returns NULL (or raises an error, depending on engine) when the divisor is zero. - The sign of the result matches the sign of the dividend: MOD(-10, 3) = -1, MOD(10, -3) = 1. ## Numeric precision - For integer types, MOD is exact. - For DOUBLE, MOD uses fmod semantics: the result is the remainder of the floating-point division truncated toward zero. Results can have surprising rounding for values that are not exactly representable in binary. - For DECIMAL, MOD preserves scale. ## Compatibility - Conforms to the SQL standard scalar function MOD with the truncated-division convention. - Matches the C % operator and typical mathematical library implementations.

Parameters

NameTypeDescription
dividendSpecifies the value to be divided. Accepts any numeric type.
divisorSpecifies the value to divide by. Must be non-zero; a zero divisor returns NULL (some engines raise an error instead).

Examples

-- Basic modulo
SELECT MOD(10, 3);
-- Result: 1
-- Negative dividend: result takes the sign of the dividend
SELECT MOD(-10, 3);
-- Result: -1
-- Negative divisor: result takes the sign of the dividend
SELECT MOD(10, -3);
-- Result: 1
-- Division by zero
SELECT MOD(10, 0);
-- Result: NULL
-- Evenly divisible
SELECT MOD(12, 4);
-- Result: 0
-- NULL propagation
SELECT MOD(NULL, 3);
-- Result: NULL

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →