0% found this document useful (0 votes)
17 views

Bit Masking

Uploaded by

Tamara White
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Bit Masking

Uploaded by

Tamara White
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Bitmasking

FOLKS Table
PersonKey Roles Name
----------- -------------------- ----------------------------------
1 3 Ray's XRay
2 1 Accurate Imaging
3 7 Universal Pictures
4 16 CIGNA
5 16 BCBS-TN

ROLES Table
RoleKey RoleDescription
----------- --------------------------------------------------------
0 Undefined
1 Facility
2 FacilityGroup
4 ParentCompany
8 PayerGroup
16 Payer
32 Team Lead
64 PSA
128 Director

SELECT *, dbo.ufsRoles(Roles) FROM Folks

RESULTS
PersonKey Roles Name
----------- -------------------- ------------------------- --------------------------------------
1 3 Ray's XRay Facility,FacilityGroup
2 1 Accurate Imaging Facility
3 7 Universal Pictures Facility,FacilityGroup,ParentCompany
4 16 CIGNA Payer
5 16 BCBS-TN Payer
Functions
CREATE FUNCTION dbo.uftBitmaskedValues (
@Value int
)
RETURNS TABLE
AS
RETURN
SELECT
POWER(2, Counter - 1) [KeyValue]
FROM
dbo.Sequence
WHERE
@Value & POWER(2, Counter - 1) = POWER(2, Counter - 1) AND
Counter < 30

CREATE FUNCTION [dbo].[ufsRoles] (


@iRoles int
)
RETURNS varchar(max)
AS
BEGIN
DECLARE @Roles varchar(max)

SELECT
@Roles = COALESCE(@Roles + ',', '') + Roles.RoleDescription
FROM
dbo.uftBitmaskedValues(@iRoles) A JOIN
dbo.Roles ON A.KeyValue=Roles.RoleKey
Return @Roles
End

Sequence Table used for function


CREATE TABLE dbo.Sequence (Counter smallint NOT NULL);
WITH Tally AS (
SELECT 1 [Tally] UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1 UNION ALL
SELECT 1
)
, Sequence AS (
SELECT
ROW_NUMBER() OVER(ORDER BY A.Tally) [Counter]
FROM
Tally A CROSS JOIN
Tally B CROSS JOIN
Tally C CROSS JOIN
Tally D
)
INSERT INTO dbo.Sequence SELECT TOP 8000 * FROM Sequence

You might also like