I assume your Permissions column is an Int. If it is, I encourage you to play around with the sample code I provide below. This should give you a clear indication of how the functionality works.
Declare @Temp Table(Permission Int, PermissionType VarChar(20))
Declare @CanRead Int
Declare @CanWrite Int
Declare @CanModify Int
Select @CanRead = 1, @CanWrite = 2, @CanModify = 4
Insert Into @Temp Values(@CanRead | @CanWrite, 'Read,write')
Insert Into @Temp Values(@CanRead, 'Read')
Insert Into @Temp Values(@CanWrite, 'Write')
Insert Into @Temp Values(@CanModify | @CanWrite, 'Modify, write')
Insert Into @Temp Values(@CanModify, 'Modify')
Select *
From @Temp
Where Permission & (@CanRead | @CanWrite) > 0
Select *
From @Temp
Where Permission & (@CanRead | @CanModify) > 0
When you use logical and, you will get a number with the 1's set appropriately based on your condition. If nothing matches, the result will be 0. If 1 or more condition matches, the result will be greater than 0.
Let me show you an example.
Suppose CanRead = 1, CanWrite = 2, and CanModify = 4. The valid combinations are:
Modify Write Read Permissions
------ ----- ---- -----------
0 0 0 Nothing
0 0 1 Read
0 1 0 Write
0 1 1 Read, Write
1 0 0 Modify
1 0 1 Modify, Read
1 1 0 Modify, Write
1 1 1 Modify, Write, Read
Now, suppose you want to test for Read or Modify. From your app, you would pass in (CanRead | CanModify). This would be 101 (in binary).
First, let's test this against a row in the table the ONLY has read.
001 (Row from table)
& 101 (Permissions to test)
------
001 (result is greater than 0)
Now, let's test against a row that only has Write.
010 (Row from table)
& 101 (Permission to test)
------
000 (result = 0)
Now test it against row that has all 3 permissions.
111 (Row from table)
& 101 (Permission to test)
------
101 (result is greater than 0)
I hope you can see that if the result of the AND operation results in a value = 0, then none of the tested permissions apply to that row. If the value is greater than 0, then at least one row is present.