In PostgreSQL, the justify_days()
function allows you to adjust an interval value, so that 30-day time periods are represented as months.
For example, if you have an interval of say, 30 days, justify_days()
will return it as 1 mon.
Syntax
The function has the following syntax:
justify_days(interval)
Where interval
is the interval that you want represented in months.
Example
Here’s a basic example to demonstrate.
SELECT justify_days(interval '30 days');
Result:
1 mon
Partial Months
Here’s an example of what happens when your interval isn’t an exact multiple of 30 days.
\x
SELECT
justify_days(interval '31 days'),
justify_days(interval '45 days'),
justify_days(interval '290 days');
Result (using vertical output):
justify_days | 1 mon 1 day justify_days | 1 mon 15 days justify_days | 9 mons 20 days
In this example I used \x
to switch over to expanded display, which displays the results using vertical output. This makes it slightly easier to read the results.
Less than a Month
If the interval is less than 30 days, then it will remain in days.
SELECT justify_days(interval '29 days');
Result (using vertical output):
29 days
Including Minutes
In this example, the interval includes a minutes part.
SELECT justify_days(interval '40 days 30 minutes');
Result (using vertical output):
1 mon 10 days 00:30:00
So in this case we end up with a time value being appended to the result.