Can I convert a bunch of boolean columns to a single bitmap in PostgreSQL?

后端 未结 3 888
暖寄归人
暖寄归人 2020-12-14 12:46

I\'d like to convert a query such as:

SELECT BoolA, BoolB, BoolC, BoolD FROM MyTable;

Into a bitmask, where the bits are defined by the val

3条回答
  •  别那么骄傲
    2020-12-14 13:20

    I came up with this approach as well. It's the most concise I could find short of writing a custom function. I'll accept this answer unless anyone has anything more clever.

    SELECT
      (BoolD::int << 0) +
      (BoolC::int << 1) +
      (BoolB::int << 2) +
      (BoolA::int << 3)
    from MyTable;
    

提交回复
热议问题