> ## Documentation Index
> Fetch the complete documentation index at: https://docs.firebolt.io/llms.txt
> Use this file to discover all available pages before exploring further.

# JUSTIFY_HOURS, JUSTIFY_DAYS, JUSTIFY_INTERVAL

> Reference material for the JUSTIFY_HOURS, JUSTIFY_DAYS, and JUSTIFY_INTERVAL functions

Normalizes an `INTERVAL` by moving whole days out of its time part, whole months out of its days part, or both. An interval keeps months, days, and time separately, so `INTERVAL '50 hours'` stays 50 hours until you justify it.

* `JUSTIFY_HOURS` turns each 24 hours of the time part into one day.
* `JUSTIFY_DAYS` turns each 30 days into one month.
* `JUSTIFY_INTERVAL` does both, then makes all three parts carry the same sign.

## Syntax

```sql theme={"theme":{"light":"css-variables","dark":"css-variables"}}
JUSTIFY_HOURS(<interval>)
JUSTIFY_DAYS(<interval>)
JUSTIFY_INTERVAL(<interval>)
```

## Parameters

| Parameter | Description | Supported input types |
| :- | :- | :- |
| `<interval>` | The interval to normalize. | `INTERVAL` |

## Return type

`INTERVAL`. If `<interval>` is `NULL`, the result is `NULL`.

## Remarks

* A day is always 24 hours and a month is always 30 days, regardless of calendar or daylight saving time. Adding a justified interval to a timestamp can therefore give a different result than adding the original one.
* `JUSTIFY_HOURS` and `JUSTIFY_DAYS` also fix the sign of the part they carry into: a positive day count with a negative time part borrows one day, and the other way around. `JUSTIFY_INTERVAL` applies the same rule across months, days, and time, so `1 mon -01:00:00` becomes `29 days 23:00:00`.
* If a carry does not fit the 32-bit day or month count, the function raises `interval out of range`.
* The three functions are compatible with PostgreSQL's functions of the same names.

## Examples

**Each function on its own**

```sql theme={"theme":{"light":"css-variables","dark":"css-variables"}}
SELECT
  JUSTIFY_HOURS(INTERVAL '50 hours') AS h,
  JUSTIFY_DAYS(INTERVAL '65 days') AS d,
  JUSTIFY_INTERVAL(INTERVAL '1 mon 65 days 50 hours') AS j;
```

| h | d | j |
| :- | :- | :- |
| 2 days 02:00:00 | 2 mons 5 days | 3 mons 7 days 02:00:00 |

**Mixed signs**

```sql theme={"theme":{"light":"css-variables","dark":"css-variables"}}
SELECT
  JUSTIFY_HOURS(INTERVAL '1 mon -1 hour') AS h,
  JUSTIFY_INTERVAL(INTERVAL '1 mon -1 hour') AS j;
```

| h | j |
| :- | :- |
| 1 mon -01:00:00 | 29 days 23:00:00 |

`JUSTIFY_HOURS` leaves the month alone, because it only moves time into days. `JUSTIFY_INTERVAL` borrows from the month so that every part is positive.

```text theme={"theme":{"light":"css-variables","dark":"css-variables"}}
JUSTIFY_HOURS / JUSTIFY_DAYS / JUSTIFY_INTERVAL (INTERVAL) -> INTERVAL
- INTERVAL has three independent parts: months (int32), days (int32),
  microseconds (int64). 1 day = 24 hours, 1 month = 30 days, fixed.
- JUSTIFY_HOURS: days += trunc(time / 24h); time %= 24h; then if days > 0 and
  time < 0 borrow one day (and vice versa). Months untouched.
- JUSTIFY_DAYS: months += trunc(days / 30); days %= 30; then if months > 0 and
  days < 0 borrow one month (and vice versa). Time untouched.
- JUSTIFY_INTERVAL: both carries, then signs of months, days and time are made
  consistent. When days and time share a sign, days are folded into months
  before the time carry, so INTERVAL '2147483647 days 24 hours' justifies to
  5965232 years 4 mons 8 days instead of overflowing.
- Overflow of the day or month count -> error "interval out of range".
  JUSTIFY_HOURS(INTERVAL '2147483647 days 25 hours') -> error.
- NULL -> NULL. An untyped string literal is read as an interval:
  JUSTIFY_HOURS('36 hours') = 1 day 12:00:00.
- Results match PostgreSQL's functions of the same names.
- Examples:
  JUSTIFY_HOURS(INTERVAL '-50 hours') = -2 days -02:00:00
  JUSTIFY_DAYS(INTERVAL '30 days 36 hours') = 1 mon 36:00:00
  JUSTIFY_INTERVAL(MAKE_INTERVAL(hours => 1000)) = 1 mon 11 days 16:00:00
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.