postgresqlrangedate-range

How does the Postgres daterange() function work?


This SQL:

select daterange(current_date, current_date);

Results in:

 daterange 
-----------
 empty
(1 row)

But the daterange() function does not seem to be documented. I've looked here and there. All I can see is that daterange() creates a daterange value with an exclusive end-date (hence the empty result).

Where can I find more about this function?

Is there a related function that can create a daterange value with an inclusive end-date?


Solution

  • PostgreSQL "daterange" is one of (built-in) rangetypes: int4range, int8range, numrange, tsrange, tstzrange, daterange.

    And daterange is discrete range - is one whose element type has a well-defined “step”, such as integer or date.

    Ranges have Inclusive and Exclusive Bounds. Default is '[)' - include lower and exclude upper bound. So

    daterange(current_date, current_date)
    

    is - from current_date to current_date excluded. This interval is "empty".

    daterange
    empty

    We can create daterange with bounds '[]'

    daterange(current_date, current_date,'[]')
    
    daterange
    [2025-08-15,2025-08-16)

    from "current_date" to "current_date+1", excluding "current_date+1".

    This is equal to

    daterange(current_date, current_date+1);
    

    or

    daterange(current_date, current_date+1,'[)');