Store multiple bit values in a single table column

后端 未结 4 664
有刺的猬
有刺的猬 2020-11-30 14:51

I need to store some sort of day-of-week scheduling in database, where I can schedule a record (say it represents a task) for a day or multiple days of the week. I need to h

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 14:57

    From the title, I was led here with a situation of having existing bit fields in a table and needing to convert to a single integer field.

    In following the example of days-of-week, let's say have 7 individual bit fields that you want to convert into a (bit-mask) integer field named Schedule:

    UPDATE T
    SET Schedule = Mon * POWER(2,0) +
                   Tue * POWER(2,1) +
                   Wed * POWER(2,2) +
                   Thu * POWER(2,3) +
                   Fri * POWER(2,4) +
                   Sat * POWER(2,5) +
                   Sun * POWER(2,6)
    

提交回复
热议问题