In PostgreSQL, the make_time()
function allows you to create a time from its hour, minute and seconds fields.
Syntax
The function has the following syntax:
make_time(hour int, min int, sec double precision)
Where hour
is the hour part, min
is the minutes part, and sec
is the seconds part.
The hour and the minutes are provided as an integer, the seconds are provided as double precision.
The result is returned as a time. More precisely, the it’s returned as time without time zone.
Example
Here’s a basic example to demonstrate.
SELECT make_time(7, 45, 15.08);
Result:
07:45:15.08
And we can verify the return type with the following query.
SELECT pg_typeof(make_time(7, 45, 15.08));
Result:
time without time zone
The pg_typeof()
function returns the data type of its argument, and so I passed make_time()
as the argument..
Out of Range
If any of the arguments are out of the range of the possible values for its date part, you’ll get an “out of range” error.
SELECT make_time(25, 45, 15.08);
Result:
ERROR: time field value out of range: 25:45:15.08
Passing Strings as Arguments
The Postgres documentation states that the arguments must be integers (and double precision in the case of the seconds
argument), but passing strings also works (probably because they’re implicitly converted to integers), as long as each argument is within its proper range.
SELECT make_time('7', '45', '15.08');
Result:
07:45:15.08
Again we can use pg_type()
to check the resulting data type.
SELECT pg_typeof(make_time('7', '45', '15.08'));
Result:
time without time zone
However, you still need to make sure each argument would be valid once converted to an integer, otherwise you’ll get an error.
SELECT make_time('25', '45', '15.08');
Result:
ERROR: time field value out of range: 25:45:15.08