©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The BIT_OR SQL Function in MySQL and MariaDB - Bitwise OR

BIT_ORSyntax:BIT_OR(Expression or column)
Return value:BIGINT
Function type:Aggregate function, Window function
Function Description

The BIT_OR() function returns the bitwise OR of all bits in "Expression or column".

The calculation is done with 64-bit precision (BIGINT).

When evaluating string arguments, the function returns 0.

Using a DISTINCT clause will result in an error.

For ENUM columns, the calculation is done from the positions of the values ​​in their lists.

For SET columns, the binary positions of the values ​​in their lists are used for the calculation = POW(2, position-1).

As an aggregate function, the BIT_OR() function can be used with a GROUP BY clause.

BIT_OR() can be used as a window function.

The BIT_OR() function returns 0 if no result is found or "Expression or column" is NULL.

SQL Examples for the BIT_OR function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

DROP TABLE IF EXISTS `test_table`;
    
CREATE TABLE `test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `type` enum('First','Second','Third','Last'DEFAULT NULL,
  `set` set('First','Second','Third','Last'DEFAULT NULL,
  `num1` int(11) NOT NULL DEFAULT 0,
  `num2` decimal(10,2) NOT NULL DEFAULT 0.00,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

INSERT INTO `test_table` (`id`,`name`,`type`,`set`,`num1`,`num2`VALUES 
(1,'Art 1','First','Last',1,7),
(2,'Art 2','Second','First,Last',3,7),
(3,'Art 3','Second','Second,Last',5,7),
(4,'Art 4','Third','First,Second,Last',5,7),
(5,'Art 5','Last','First,Last',7,15),
(6,'Art 6','First','First,Last',7,15),
(7,'Art 7','Third','First,Third,Last',9,15),
(8,'Art 8','Last','Second,Third,Last',9,15);

select * from `test_table`;

select BIT_OR(name) from test_table;

select BIT_OR(num1),BIT_OR(num2),BIT_OR(`type`),BIT_OR(`set`from test_table;
select BIT_OR(num1),BIT_OR(num2),BIT_OR(`type`),BIT_OR(`set`from test_table where `type`='third';

select bin(BIT_OR(num1)),bin(BIT_OR(num2)) from test_table where id>8;

/* as Window function */

SELECT name, `type`, num1, BIT_OR(num2) 
  OVER (PARTITION BY `type`AS `bit_or` FROM test_table;
  
select BIT_OR(null), bin(BIT_OR(null));

id
name
type
set
num1
num2
int(11)varchar(20) LATIN1enum LATIN1set LATIN1int(11)decimal(10,2)
1Art 1FirstLast17.00
2Art 2SecondFirst,Last37.00
3Art 3SecondSecond,Last57.00
4Art 4ThirdFirst,Second,Last57.00
5Art 5LastFirst,Last715.00
6Art 6FirstFirst,Last715.00
7Art 7ThirdFirst,Third,Last915.00
8Art 8LastSecond,Third,Last915.00
bit_or(name)
bigint(21)
0
BIT_OR(num1)
BIT_OR(num2)
BIT_OR(`type`)
BIT_OR(`set`)
bigint(21)bigint(21)bigint(21)bigint(21)
1515715
bit_or(num1)
bit_or(num2)
bit_or(`type`)
bit_or(`set`)
bigint(21)bigint(21)bigint(21)bigint(21)
1315315
bin(bit_or(num1))
bin(bit_or(num2))
varchar(64) BINARYvarchar(64) BINARY
00
name
type
num1
bit_or
test_table
varchar(20) LATIN1
test_table
enum LATIN1
test_table
int(11)

bigint(21)
Art 6First715
Art 1First115
Art 3Second57
Art 2Second37
Art 4Third515
Art 7Third915
Art 8Last915
Art 5Last715
bit_or(null)
bin(bit_or(null))
bigint(21)varchar(64) BINARY
00
The examples were created with the MyWAY SQL manager: Download

How to use the BIT_OR() function in MySQL and MariaDB databases

The BIT_OR() function in MySQL and MariaDB is used to do a bitwise OR operation on a set of values ​​and return the result as an integer, which can be useful in various applications, such as working with binary flags, combining or merging binary values, or performing bitwise calculations. The function works with whole numbers and treats the input values ​​as binary representations.

Bitwise operations and working with binary values ​​with this function include use cases such as combining or merging binary flags, combining multiple flags into a single value, or to enable or set specific bits within a binary value, or to perform bitwise calculations, for example by combining binary values ​​or calculating masks or implementing custom logic based on binary representations.
BIT_ANDBIT_COUNTBIT_LENGTHBIT_ORBIT_XORCASTCEIL

Further MySQL und MariaDB SQL Aggregate functions

ANY_VALUEANY_VALUE(Column)
More about ANY_VALUE Function

AVGAVG(Expression or column)
More about AVG Function

BIT_ANDBIT_AND(Expression or column)
More about BIT_AND Function

BIT_XORBIT_XOR(Expression or column)
More about BIT_XOR Function

COUNTCOUNT(Expression or column)
More about COUNT Function

GROUP_CONCATGROUP_CONCAT([DISTINCT] Expression [, Expression ...] [ORDER BY ... [ASC | DESC]] [SEPARATOR ...] [LIMIT ...])
More about GROUP_CONCAT Function

JSON_ARRAYAGGJSON_ARRAYAGG(Expression or column)

JSON_OBJECTAGGJSON_OBJECTAGG(Expression or column)

MAXMAX(Expression or column)
More about MAX Function

MINMIN(Expression or column)
More about MIN Function

SUMSUM(Expression or column)
More about SUM Function

STDSTD(Expression or column)
More about STD Function

STDDEVSTDDEV(Expression or column)
More about STDDEV Function

STDDEV_POPSTDDEV_POP(Expression or column)
More about STDDEV_POP Function

STDDEV_SAMPSTDDEV_SAMP(Expression or column)
More about STDDEV_SAMP Function

VAR_POPVAR_POP(Expression or column)
More about VAR_POP Function

VAR_SAMPVAR_SAMP(Expression or column)
More about VAR_SAMP Function

VARIANCEVARIANCE(Expression or column)
More about VARIANCE Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the BIT_OR SQL function: mysql.com and mariadb.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer