Database data structure using powers of two

I am designing a data structure and want to know if I am missing something doing it this way.

Let's say I have a DAY column of type int.

1  : Monday
2  : Tuesday
4  : Wednesday
8  : Thursday
16 : Friday
32 : Saturday
64 : Sunday

If I wanted to store Monday and Friday, I would enter 17 in the DAY column. If I wanted to store Tuesday and Wednesday, I would enter 6, etc.

Is this a valid way to store data. What the query would look like if I wanted to choose where the entry contained Saturday and any change of days or Saturday, and not Wednesday. Is it possible? Will it be fast?

What is called this concept?

+3
source share
3 answers

, "" , , , -:

-- Contains Saturday and any other combination of days
SELECT * FROM Table
WHERE (DayBitColumn & 32) = 32

-- Contains Saturday and any other combination of days, except Wednesday
SELECT * FROM Table
WHERE (DayBitColumn & 32) = 32 AND (DayBitColumn & 4) = 0

: @Andriy M, :

SELECT * FROM Table
WHERE (DayBitColumn & 36) = 32

[ '&' ]

+3

:

?
?

, .
- .

, SQL Server - , , , . ?

, .

  • : - ,
  • : , , .

, , , Event_Day -

EventID | Day
1         2
1         4

, , . , , PIVOT , .

+2

1) ? . , . , . -, , , .

2) ? . WHERE skip = 0? . WHERE skip & 4 = 4... , , .

, , , , . , , , , >= 64.

, . , . , , . , ... + -, , , . , ..

+1

Source: https://habr.com/ru/post/1792955/


All Articles