PostgreSQL bit_or() Function
The PostgreSQL bit_or() function is an aggregate function that performs a “bitwise OR” of all non-null input values.
The bitwise OR processes two binary numbers of the same length, if the two corresponding binary bits are 0, the result value of the bit is 0, otherwise it is 1.
bit_or() Syntax
Here is the syntax of the PostgreSQL bit_or() function:
bit_or(expr)
Typically, we use the bit_or() function like:
SELECT bit_or(expr), ...
FROM table_name
[WHERE ...]
[GROUP BY group_expr1, group_expr2, ...];
Parameters
expr-
Required. A column name or expression. It accepts a value of type integer or bit.
Return value
The PostgreSQL bit_or() function returns values of the same type as the input parameters, and it returns the result of performing a “bitwise OR” operation on all non-null input values.
Note that the bit_or() function only handles non-null values. That is, null values are ignored by the bit_or() function.
bit_or() Examples
To demonstrate usages of the PostgreSQL bit_or() function, we simulate a temporary table using the following statement with UNION and SELECT:
SELECT 4 x
UNION
SELECT 5 x
UNION
SELECT 6 x;
x
---
4
6
5
(3 rows)The following statement performs the bit_or() operation to the values of the x column:
SELECT bit_or(x)
FROM (
SELECT 4 x
UNION
SELECT 5 x
UNION
SELECT 6 x
) t;
bit_or
--------
7
(1 rows)Here, the function bit_or() performs the “bitwise OR” operation on the values (4, 5, 6) in the x column, and the calculation steps are as follows:
4 -> 100
5 -> 101
6 -> 110
bit_or() = 111 = 7So the bit_or() function returns 7.