Nik Kashi
Nik Kashi

Reputation: 4606

Set a specific bit of a number in Oracle database

How can I set a specific bit of a number in Oracle with SQL command? there is only BITAND operator

BITAND (DEPENDENCY_MAP, 2)

In DEPENDENCY_MAP every bit defines one type dependency. With this command I can find second bit is seted or not., but how can I modify this bit?

Upvotes: 3

Views: 3878

Answers (1)

Klas Lindbäck
Klas Lindbäck

Reputation: 33273

To set the bit:

UPDATE <table name>
   SET DEPENDENCY_MAP = DEPENDENCY_MAP + 2 -- Set the bit (the row must not have the bit set already)
 WHERE BITAND (DEPENDENCY_MAP, 2) = 0 -- This will match rows that don't have the bit set
   AND <add your own row filters>

To clear the bit:

UPDATE <table name>
   SET DEPENDENCY_MAP = DEPENDENCY_MAP - 2 -- Clear the bit (the row must have the bit set already)
 WHERE BITAND (DEPENDENCY_MAP, 2) > 0 -- This will match rows that do have the bit set
   AND <add your own row filters>

NOTE: The code above works for positive numbers. I haven't bothered to figure out what would happen if DEPENDENCY_MAP is a negative number, since I assume you don't have negative numbers.

Upvotes: 7

Related Questions