Postgresql Div Function

PostgreSQL: div Function

In this PostgreSQL post explains how to use the PostgreSQL div function with syntax and examples.

Description

The PostgreSQL div function is used for integer division where n is divided by m and an integer value is returned.

Syntax

The syntax for the div function in PostgreSQL is:

div(n, m)

Parameters or Arguments

n

A numeric value that will be divided by m.

m

The number used to divide into n.

Note

  • The div function will return the result as an integer value.
  • See also the mod and floor function.

Applies To

The div function can be used in the following versions of PostgreSQL:

  • PostgreSQL 9.4, PostgreSQL 9.3, PostgreSQL 9.2, PostgreSQL 9.1, PostgreSQL 9.0, PostgreSQL 8.4

Example

Let's look at some PostgreSQL div function examples and explore how to use the div function in PostgreSQL.

For example:

postgres=# SELECT div(9, 2);
 div
-----

   4
(1 row)

postgres=# SELECT div(8, 2);
 div
-----

   4
(1 row)

postgres=# SELECT div(10.5, 3.1);
 div
-----

   3
(1 row)

postgres=# SELECT div(10.5, -3.1);
 div
-----

  -3
(1 row)