Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6787839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:24:50+00:00 2026-05-26T17:24:50+00:00

I need to extract data from a third party system (I have no influence

  • 0

I need to extract data from a third party system (I have no influence over its design). It’s a SQL Server 2005 database uses bitmaps to store user privileges. It has five INT fields giving a maximum of 5 * 32 = 160 privileges. It stores a number of types of privilege and re-uses the bitmaps for each type. So in total there are 6 fields that drive privileges. Each privilege can be assigned to a specific item of a given type.

An example of a type is “table” so items in that context would be table names.

The privilege table looks like this:

ID | PRIVTYPE | USERNAME | ITEMNAME | BITMAP1 | BITMAP2 | BITMAP3 | BITMAP4 | BITMAP5

For example

123 | Table | Joe | Customers | 0x408 | 0x1 | 0x5c | 0x1000 | 0x0

Another table contains the privileges represented by each bit. It looks like this:

PRIVTYPE | BITMAP_ID | BITVALUE | PRIVILEGE_NAME

For example, entries relating to the above bitmaps would be:

Table | 1 |0x8   | View
Table | 1 |0x400 | Edit
Table | 2 |0x1   | Report
Table | 3 |0x4   | View Address Data
Table | 3 |0x8   | View Order Data
Table | 3 |0x10  | View Payment Data
Table | 3 |0x40  | View System Data
Table | 4 |0x1000| View Hidden Fields

I want to somehow parse the privilege table into a new table or view that will have one record per user per item privilege. Like this:

USERNAME | ITEMNAME |PRIVILEGE_NAME
Joe | Table | Customers | View
Joe | Table | Customers | Edit
Joe | Table | Customers | Report
Joe | Table | Customers | view Address Data
Joe | Table | Customers | view Order Data
Joe | Table | Customers | view Payment Data
Joe | Table | Customers | view System Data
Joe | Table | Customers | view Hidden Fields

I think I need to create a view by running a select statement that will return multiple rows for each row in the privilege table: one row for every set bit in a bitmask field. So, for example, a single row in the privilege table that has 3 bits set in the bitmasks will cause three rows to be returned.

I have searched for answers about breaking tables into multiple rows. I’ve looked at various joins and pivots but I can’t find something that will do what I need. Is the above possible? Any guidance appreciated…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:24:51+00:00Added an answer on May 26, 2026 at 5:24 pm

    You could unpivot the first table (called @UserPrivileges below) and join it to the second one (@Privileges) on privilege type, bitmap ID and the result of bitwise AND between the bitmap in the first table and BITVALUE in the second table.

    Below is my implementation.

    Setup:

    DECLARE @UserPrivileges TABLE (
      ID int,
      PRIVTYPE varchar(50),
      USERNAME varchar(50),
      ITEMNAME varchar(50),
      BITMAP1 int,
      BITMAP2 int,
      BITMAP3 int,
      BITMAP4 int,
      BITMAP5 int
    );
    INSERT INTO @UserPrivileges
      (ID, PRIVTYPE, USERNAME, ITEMNAME, BITMAP1, BITMAP2, BITMAP3, BITMAP4, BITMAP5)
    SELECT 123, 'Table', 'Joe', 'Customers', 0x408, 0x1, 0x5c, 0x1000, 0x0
    ;
    
    DECLARE @Privileges TABLE (
      PRIVTYPE varchar(50),
      BITMAP_ID int,
      BITVALUE int,
      PRIVILEGE_NAME varchar(50)
    );
    INSERT INTO @Privileges (PRIVTYPE, BITMAP_ID, BITVALUE, PRIVILEGE_NAME)
    SELECT 'Table', 1, 0x8   , 'View              ' UNION ALL
    SELECT 'Table', 1, 0x400 , 'Edit              ' UNION ALL
    SELECT 'Table', 2, 0x1   , 'Report            ' UNION ALL
    SELECT 'Table', 3, 0x4   , 'View Address Data ' UNION ALL
    SELECT 'Table', 3, 0x8   , 'View Order Data   ' UNION ALL
    SELECT 'Table', 3, 0x10  , 'View Payment Data ' UNION ALL
    SELECT 'Table', 3, 0x40  , 'View System Data  ' UNION ALL
    SELECT 'Table', 4, 0x1000, 'View Hidden Fields'
    ;
    

    Query:

    WITH unpivoted AS (
      SELECT
        ID,
        PRIVTYPE,
        USERNAME,
        ITEMNAME,
        RIGHT(BITMAP_ID, 1) AS BITMAP_ID,  -- OR: STUFF(BITMAP_ID, 1, 6, '')
                                           -- OR: SUBSTRING(BITMAP_ID, 7, 999)
                                           -- OR: REPLACE(BITMAP_ID, 'BITMAP', '')
        BITMAP_VAL
      FROM UserPrivileges
      UNPIVOT (
        BITMAP_VAL FOR BITMAP_ID IN (
          BITMAP1, BITMAP2, BITMAP3, BITMAP4, BITMAP5
        )
      ) u
    ),
    joined AS (
      SELECT
        u.USERNAME,
        u.PRIVTYPE,
        u.ITEMNAME,
        p.PRIVILEGE_NAME
      FROM unpivoted u
        INNER JOIN Privileges p
           ON u.PRIVTYPE = p.PRIVTYPE
          AND u.BITMAP_ID = p.BITMAP_ID
          AND u.BITMAP_VAL & p.BITVALUE <> 0
    )
    SELECT * FROM joined
    

    Results:

    USERNAME  PRIVTYPE  ITEMNAME   PRIVILEGE_NAME
    --------  --------  ---------  ------------------
    Joe       Table     Customers  View              
    Joe       Table     Customers  Edit              
    Joe       Table     Customers  Report            
    Joe       Table     Customers  View Address Data 
    Joe       Table     Customers  View Order Data   
    Joe       Table     Customers  View Payment Data 
    Joe       Table     Customers  View System Data  
    Joe       Table     Customers  View Hidden Fields
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pgp-encrypted file that I need to extract data from at runtime.
I have two databases with equivalent structure and I need to extract data from
I have a table that I need to extract data from, and wish to
I've got and sql express database I need to extract some data from. I
I need to extract data from a .mpp file on the network and combine
I need to extract data from a structure and put it into a list,
I need to extract data from a source that presents it in one of
Sometimes I need to quickly extract some arbitrary data from XML files to put
I have an Indian company data set and need to extract the City and
I have a string in c# containing some data i need to extract based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.