How DIV Works in MariaDB

In MariaDB, DIV performs integer division.

It discards from the division result any fractional part to the right of the decimal point.

Syntax

The syntax goes like this:

DIV

Where the number to divide is on the left and the number to divide it by is on the right.

Example

Here’s an example:

SELECT 100 DIV 2;

Result:

+-----------+
| 100 DIV 2 |
+-----------+
|        50 |
+-----------+

Fractional Results

As mentioned, DIV discards from the division result any fractional part to the right of the decimal point.

Example:

SELECT 100 DIV 3;

Result:

+-----------+
| 100 DIV 3 |
+-----------+
|        33 |
+-----------+

Division by Zero

If the ERROR_FOR_DIVISION_BY_ZERO SQL mode is not set, division by zero returns NULL with a warning. If set, division by zero returns an error if one tries to update a column with 1/0 and returns a warning as well.

Here’s an example of a NULL result:

SELECT 100 DIV 0;

Result:

+-----------+
| 100 DIV 0 |
+-----------+
|      NULL |
+-----------+
1 row in set, 1 warning (0.001 sec)

Let’s check the warning:

SHOW WARNINGS;

Result:

+---------+------+---------------+
| Level   | Code | Message       |
+---------+------+---------------+
| Warning | 1365 | Division by 0 |
+---------+------+---------------+

Invalid Operands

Using invalid operands results in NULL with a warning:

SELECT 'One' DIV 'Two';

Result:

+-----------------+
| 'One' DIV 'Two' |
+-----------------+
|            NULL |
+-----------------+
1 row in set, 3 warnings (0.013 sec)

Check the warnings:

SHOW WARNINGS;

Result:

+---------+------+------------------------------------------+
| Level   | Code | Message                                  |
+---------+------+------------------------------------------+
| Warning | 1292 | Truncated incorrect DECIMAL value: 'One' |
| Warning | 1292 | Truncated incorrect DECIMAL value: 'Two' |
| Warning | 1365 | Division by 0                            |
+---------+------+------------------------------------------+