PostgreSQL make_interval() Function
The PostgreSQL make_interval()
function creates an interval value from the given year, month, day, hour, minute, and second fields.
make_interval()
Syntax
Here is the syntax of the PostgreSQL make_interval()
function:
make_interval(
[ years int
[, months int
[, weeks int
[, days int
[, hours int
[, mins int
[, secs double precision ]]]]]]]
) -> TIMESTAMP
Parameters
years
-
Required. The number of Years. The default value is 0.
months
-
Required. The number of months. The default value is 0.
weeks
-
Required. The number of weeks. The default value is 0.
days
-
Required. The number of days. The default value is 0.
hours
-
Required. The number of hours. The default value is 0.
mins
-
Required. The number of minutes. The default value is 0.
secs
-
Required. The number of seconds. The default value is 0.
Return value
The PostgreSQL make_interval()
function returns an interval value created from the given year, month, day, hour, minute, and second fields.
make_interval()
Examples
Here are a few examples showing the basic usage of the make_interval()
function.
SELECT make_interval(1, 2, 3, 4, 5, 6, 1.123);
make_interval
------------------------------------
1 year 2 mons 25 days 05:06:01.123
You can specify a single field to create an interval value, such as one year, two months, one hour, etc.:
SELECT
make_interval(years => 2) AS "years => 2",
make_interval(months => 2) AS "months => 2",
make_interval(weeks => 2) AS "weeks => 2",
make_interval(days => 2) AS "days => 2",
make_interval(hours => 2) AS "hours => 2",
make_interval(mins => 2) AS "mins => 2",
make_interval(secs => 2) AS "secs => 2";
-[ RECORD 1 ]---------
years => 2 | 2 years
months => 2 | 2 mons
weeks => 2 | 14 days
days => 2 | 2 days
hours => 2 | 02:00:00
mins => 2 | 00:02:00
secs => 2 | 00:00:02
You can also combine several parameters arbitrarily, for example:
SELECT make_interval(years => 1, days => 10, mins => 15);
make_interval
-------------------------
1 year 10 days 00:15:00