In this guide, we will explain how to use the MySQL MOD function with syntax and examples.
Description
The MOD function returns the remainder of n divided by m.
Syntax
The syntax for the MOD function is:
MOD( n, m )
OR
n MOD m
OR
n % m
Parameters or Arguments
n
The value that will be divided by m.
m
The value that will be divided into n.
Note
- The MOD function uses the formula of
n / m
and the remainder is what is returned. - Starting in MySQL 4.1.7, the MOD function returns the exact remainder (without any rounding).
- Before MySQL 4.1.7, the MOD function rounds the remainder to an integer value.
- The MOD function syntax of
n mod m
was introduced in MySQL 4.1.
Applies To
The MOD function can be used in the following versions :
- MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23
Example
Let’s look at some MOD function examples and explore how to use the MOD function.
For example:
mysql> SELECT MOD(12, 5); Result: 2 mysql> SELECT MOD(12, 0.18); Result: 0.12 mysql> SELECT MOD(100, 3.5938); Result: 2.9674
mysql> SELECT 12 MOD 5; Result: 2 mysql> SELECT 12 MOD 0.18; Result: 0.12 mysql> SELECT 100 MOD 3.5938; Result: 2.9674
mysql> SELECT 12 % 5; Result: 2 mysql> SELECT 12 % 0.18; Result: 0.12 mysql> SELECT 100 % 3.5938; Result: 2.9674
Next Topic : Click Here
Pingback: MySQL: MIN Function | Adglob Infosystem Pvt Ltd