How to Check if the Upper Bound of the Given Range is Inclusive or Exclusive

PostgreSQL offers a variety of built-in functions that are specifically designed for working with range types. These functions assist us in dealing with ranges efficiently. For instance, the lower_inc() function informs us if the lower bound is inclusive, the upper_inc() function checks if the upper bound of the specified range is inclusive, the upper() function retrieves the upper bound of the function, and so on.

This post illustrates how to check if the upper bound of the specified range is included or excluded.

How to Check if the Upper Bound of the Given Range is Inclusive or Exclusive

In PostgreSQL, the UPPER_INC() function accepts a range or multi-range as an argument and checks if the upper bound of the specified range is included in the range or not.

Syntax

Use the below-stated syntax to execute the UPPER_INC() function on the specified range:

UPPER_INC(range | multirange);

Parameters

This function accepts a single-range or multi-range value as an argument.

Return Type

The return type of the UPPER_INC() function is Boolean.

Return Value

It retrieves a True or False value which shows if the upper bound of the given range is inclusive or exclusive.

Example 1

This example demonstrates how to use the UPPER_INC() function to check if the lower bound is included in the range or not:

SELECT UPPER_INC(numrange'[0,10]')AS upper_bound_inclusive;

The output snippet shows “true” which indicates that the upper bound is included in the range:

img

Example 2

Let’s try out another example for a better understanding of the UPPER_INC() function in Postgres:

SELECT UPPER_INC(numrange'[0,10)')AS upper_bound_inclusive;

The output snippet shows “false” which indicates that the upper bound is not included in the range:

img

Example 3

In the following example, the UPPER_INC() function is applied on the range “[5, 6)”:

SELECT UPPER_INC(numrange'(5, 6]') AS upper_bound_inclusive;

The output snippet shows that the upper bound is included in the range:

img

This is how the UPPER_INC() function works in PostgreSQL.

Conclusion

In PostgreSQL, the UPPER_INC() function accepts a range or multi-range as an argument and checks if the upper bound of the specified range is included in the range or not. The return type of the UPPER_INC() is Boolean. It retrieves a True or False value which shows if the upper bound of the given range is inclusive or exclusive. This post has demonstrated the various use cases of the UPPER_INC() function in PostgreSQL using practical examples.