PostgreSQL offers various inbuilt range functions for efficient manipulation of ranges. These functions serve specific purposes related to range types. For example, the isempty() function checks for an empty range, lower_inc() checks if the lower bound is inclusive, the lower() function returns the lower bound of the range, and so on.
This write-up illustrates a complete guide on the usage of the ISEMPTY() function in PostgreSQL.
What Does ISEMPTY() Function Do in PostgreSQL?
In PostgreSQL, the ISEMPTY() is an in-built range function that accepts a range or multi-range as an argument and retrieves a TRUE or FALSE value. The TRUE values state that the given range is empty while the FALSE value indicates that the specified range/multi-range is not empty.
Syntax
Use the below-stated syntax to check if the given range/multi-range is empty or not:
ISEMPTY(range | multirange);
Parameters
The stated function accepts a single-range or multi-range value as an argument.
Return Value
It retrieves a True or False value which shows if the given range is empty or not.
Return Type
The return type of the stated function is Boolean.
Example 1
In the following example, the ISEMPTY() function is applied to a specific range to check if it is empty or not:
SELECT ISEMPTY('(2, 4]'::int4range);
Here in the above example,
- The “(2, 4]” represents a range that contains the integer values between the following range “> 2” and “<= 4”.
- The "::int4range" is used to explicitly specify that the given range is of INTEGER data type.
The stated function retrieves “false”, which means the given range is not empty.
Example 2
In the following example, the ISEMPTY() function is applied to a specific range to check if it is empty or not:
SELECT ISEMPTY('(2, 3)'::int4range);
Here in the above example,
- The “(2, 3)” represents a range that contains the integer values between the following range “> 2” and “<3”.
- The ISEMPTY() function will return true as output because there are no integer values between 2 (exclusive) and 3 (exclusive) in the specified range.
The “true” value in the output demonstrates that the specified range is empty.
Conclusion
In PostgreSQL, the ISEMPTY() is an in-built range function that accepts a range or multi-range as an argument and retrieves a TRUE or FALSE value. The TRUE values state that the given range is empty while the FALSE value indicates that the specified range/multi-range is not empty. This post has illustrated a couple of use cases of the ISEMPTY() function in PostgreSQL.