Summary: In this tutorial, you’ll learn how to use the PostgreSQL JUSTIFY_HOURS function to adjust an interval by converting 24-hour periods to days.
PostgreSQL JUSTIFY_HOURS Function Overview #
The JUSTIFY_HOURS function adjusts an interval by converting 24-hour periods to days.
Syntax
JUSTIFY_HOURS (value)Code language: JavaScript (javascript)The JUSTIFY_HOURS function accepts one parameter value, which is the interval you want to adjust.
The JUSTIFY_HOURS function checks the value interval and converts hours that exceed 24 to days. It does not change the minutes and seconds.
The JUSTIFY_HOURS function returns NULL if the input interval is NULL.
Basic PostgreSQL JUSTIFY_HOURS Function Example #
The following example uses the JUSTIFY_HOURS function to adjust 48 hours:
SELECT
JUSTIFY_HOURS(INTERVAL '48 hours') result;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)Output:
result
--------
2 daysCode language: plaintext (plaintext)Since 48 hours is 2 days, the function converts it to 2 days.
Using PostgreSQL JUSTIFY_HOURS() Function with Intervals That Are Not Multiple of 24 Hours #
The following statement uses the JUSTIFY_HOURS function to convert 50 hours to days:
SELECT JUSTIFY_HOURS(INTERVAL '50 hours') result;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)Output:
result
-----------------
2 days 02:00:00Code language: plaintext (plaintext)Using PostgreSQL JUSTIFY_HOURS() Function with Intervals That Include Minutes and Seconds #
The following statement uses the JUSTIFY_HOURS function to convert an interval that includes minutes and seconds:
SELECT
JUSTIFY_HOURS(INTERVAL '50 hours 45 minutes 30 seconds') result;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)Output:
result
-----------------
2 days 02:45:30Code language: plaintext (plaintext)In this example, the JUSTIFY_HOURS function converts 50 hours to 2 days and 2 hours. It does not adjust the minutes and seconds.
Summary #
- Use the PostgreSQL
JUSTIFY_HOURSfunction to adjust an interval by converting excess hours to days.