PostgreSQL JUSTIFY_HOURS() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL JUSTIFY_HOURS()
function to adjust 24-hour intervals as days.
Introduction to the PostgreSQL JUSTIFY_HOURS() function
The JUSTIFY_HOURS()
function normalizes an interval by converting hours exceeding 24 to days.
Here’s the syntax of the JUSTIFY_HOURS()
function:
In this syntax:
value
is an interval value you want to justify.
The JUSTIFY_HOURS()
function returns an adjusted interval with:
- Hours exceeding 24 are converted to days.
- The remaining hours are kept the same.
- Minutes, seconds, and other units remain unchanged.
If the value is NULL
, the function returns NULL
.
PostgreSQL JUSTIFY_HOURS() function examples
Let’s take some examples of using the PostgreSQL JUSTIFY_HOURS()
function.
1) Basic PostgreSQL JUSTIFY_HOURS() function example
The following statement uses the JUSTIFY_HOURS()
function to adjust intervals that are multiples of 24 hours:
Output:
2) Using JUSTIFY_HOURS() function with intervals that are not multiple of 24 hours
The following example uses the JUSTIFY_HOURS()
function to adjust intervals that are not multiples of 24 hours:
Output:
3) Using the JUSTIFY_HOURS() function with intervals that include hours
The following example uses the JUSTIFY_HOURS()
function to adjust intervals that include hours, minutes, and seconds:
Output:
Summary
- Use the
JUSTIFY_HOURS()
function to adjust 24-hour intervals as days.