Skip to content
Advertisement

How do I group “or” in pl/sql

I have legacy sql query that selects bit masks (among other data), something like:

1
2
1

How do I group this output like:

1 or 2 or 1 

That should be 3

Advertisement

Answer

In order to do bit-wise logic you have to do a “bit” of math. (Bad puns are free around here :-).

Oracle defines the BITAND function. To get a bitwise ‘or’ you can define your own function as:

FUNCTION BITOR(n1 IN NUMBER, n2 IN NUMBER) 
  RETURN NUMBER
IS
BEGIN
  RETURN n1 - BITAND(n1, n2) + n2;
END BITOR;

And for completeness, BITXOR is

FUNCTION BITXOR(n1 IN NUMBER, n2 IN NUMBER)
  RETURN NUMBER
IS
BEGIN
  RETURN BITOR(n1, n2) - BITAND(n1, n2);
END BITXOR;

Best of luck.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement