How to make a bit flags selection filter?

Asked

Viewed 131 times

0

I am a database novice and am participating in an Oracle-SQL project where I need to create a flags selection filter in a column that stores flags as active or not from the position of each binary number character, which apparently is called bit flag.

For example:

In a hypothetical situation of 4 flags, the column Flag store the numbers like this:

  • 11, for active flags 1 and 2;

  • 1100, for flags 3 and 4 active;

  • 1111, for the case of all active flags;

  • 0, for no active flag;

Through SQL (Oracle), how can I manipulate these numbers in order to know, for example, if flag 3 is active, bearing in mind that other flags may be active and that this table has a lot of elements to go through?

  • Wouldn’t it be better to do by position ? 0010 , 3 active , 1101 1,2 and 4 active ? But it would be better to normalize this, see Normal Forms of Database.

  • Your suggestion makes perfect sense, but the problem is I can’t make any changes at the bank.

1 answer

0

For those who have the same problem as mine, I solved by using the SUBSTR function of SQL, allied to the position parameter receiving negative value to reverse the initial position.

Ex:

SELECT * FROM TABELA_COM_FLAGS
WHERE SUBSTR(COLUNA_FLAG, -1, 1) = 1

returns all elements where the first binary (i.e., the last digit of the number) of the COLUNA_FLAG is active.

More information at this link.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.