Bitwise AND operator
1. Get Active Categories
SQL Server Query 1
-- Bit 1: 1 = Active, 0 = Inactive
-- Bit 2: 1 = Seasonal, 0 = Non-seasonal
-- Bit 3: 1 = Featured, 0 = Not featured
-- 1. Get Active Categories
SELECT Categories.CategoryID, Categories.CategoryName
FROM Categories, (VALUES
(1, 3), -- 1 = CategoryID, 3 = BitFlag: Active and Seasonal (1 | 2 = 3)
(2, 5), -- 2 = CategoryID, 5 = BitFlag: Active and Featured (1 | 4 = 5)
(3, 0) -- 3 = CategoryID, 0 = BitFlag: Inactive and Nonseasonal and Not Feature
) AS CategoryFeature (CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID AND
(CategoryFeature.BitFlag & 1) = 1; -- Bitwise AND with 1 (0001)
Create SQL query with SqlQueryBuilder 1
var (sql1, parameters1) = new SqlQueryBuilder()
.Select()
.Columns("Categories.CategoryID", "Categories.CategoryName")
.From(new Table("Categories"), new VALUES(new List<List<object>>()
{
new List<object>() {1, 3 },
new List<object>() {2, 5 },
new List<object>() {3, 0 },
}, "CategoryFeature", "CategoryID", "BitFlag"))
.Where(new Where(new Column("Categories.CategoryID").Equale(new Column("CategoryFeature.CategoryID")))
.AND(new ColumnArithmatic().StartBracket("CategoryFeature.BitFlag").BitwiseAND(1).EndBracket().Equale(1))
)
.Build();
Query build by SqlQueryBuilder 1
SELECT Categories.CategoryID,
Categories.CategoryName
FROM Categories, (VALUES (@pMAIN_2606200149202747220, @pMAIN_2606200149202747221), (@pMAIN_2606200149202747222, @pMAIN_2606200149202747223), (@pMAIN_2606200149202747224, @pMAIN_2606200149202747225)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2606200149202747226) = @pMAIN_2606200149202747227;
Parameters (If used)
| Name |
Value |
| @pMAIN_2606200149202747220 |
1 |
| @pMAIN_2606200149202747221 |
3 |
| @pMAIN_2606200149202747222 |
2 |
| @pMAIN_2606200149202747223 |
5 |
| @pMAIN_2606200149202747224 |
3 |
| @pMAIN_2606200149202747225 |
0 |
| @pMAIN_2606200149202747226 |
1 |
| @pMAIN_2606200149202747227 |
1 |
Query Results 1:
| |
CategoryID |
CategoryName |
| 1 |
1
|
Beverages
|
| 2 |
2
|
Condiments
|
2. Get Seasonal Categories
SQL Server Query 2
-- Bit 1: 1 = Active, 0 = Inactive
-- Bit 2: 1 = Seasonal, 0 = Non-seasonal
-- Bit 3: 1 = Featured, 0 = Not featured
-- 2. Get Seasonal Categories
SELECT Categories.CategoryID, Categories.CategoryName
FROM Categories, (VALUES
(1, 3), -- 1 = CategoryID, 3 = BitFlag: Active and Seasonal (1 | 2 = 3)
(2, 5), -- 2 = CategoryID, 5 = BitFlag: Active and Featured (1 | 4 = 5)
(3, 0) -- 3 = CategoryID, 0 = BitFlag: Inactive and Nonseasonal and Not Feature
) AS CategoryFeature (CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID AND (CategoryFeature.BitFlag & 2) = 2; -- Bitwise AND with 2 (0010)
Create SQL query with SqlQueryBuilder 2
var (sql2, parameters2) = new SqlQueryBuilder()
.Select()
.Columns("Categories.CategoryID", "Categories.CategoryName")
.From(new Table("Categories"), new VALUES(new List<List<object>>()
{
new List<object>() {1, 3 },
new List<object>() {2, 5 },
new List<object>() {3, 0 },
}, "CategoryFeature", "CategoryID", "BitFlag"))
.Where(new Where(new Column("Categories.CategoryID").Equale(new Column("CategoryFeature.CategoryID")))
.AND(new ColumnArithmatic().StartBracket("CategoryFeature.BitFlag").BitwiseAND(2).EndBracket().Equale(2))
)
.Build();
Query build by SqlQueryBuilder 2
SELECT Categories.CategoryID,
Categories.CategoryName
FROM Categories, (VALUES (@pMAIN_2606200149202855310, @pMAIN_2606200149202855311), (@pMAIN_2606200149202855312, @pMAIN_2606200149202855313), (@pMAIN_2606200149202855314, @pMAIN_2606200149202855315)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2606200149202855316) = @pMAIN_2606200149202855317;
Parameters (If used)
| Name |
Value |
| @pMAIN_2606200149202855310 |
1 |
| @pMAIN_2606200149202855311 |
3 |
| @pMAIN_2606200149202855312 |
2 |
| @pMAIN_2606200149202855313 |
5 |
| @pMAIN_2606200149202855314 |
3 |
| @pMAIN_2606200149202855315 |
0 |
| @pMAIN_2606200149202855316 |
2 |
| @pMAIN_2606200149202855317 |
2 |
Query Results 2:
| |
CategoryID |
CategoryName |
| 1 |
1
|
Beverages
|
3. Get Featured Categories
SQL Server Query 3
-- Bit 1: 1 = Active, 0 = Inactive
-- Bit 2: 1 = Seasonal, 0 = Non-seasonal
-- Bit 3: 1 = Featured, 0 = Not featured
-- 3. Get Featured Categories
SELECT Categories.CategoryID, Categories.CategoryName
FROM Categories, (VALUES
(1, 3), -- 1 = CategoryID, 3 = BitFlag: Active and Seasonal (1 | 2 = 3)
(2, 5), -- 2 = CategoryID, 5 = BitFlag: Active and Featured (1 | 4 = 5)
(3, 0) -- 3 = CategoryID, 0 = BitFlag: Inactive and Nonseasonal and Not Feature
) AS CategoryFeature (CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID AND (CategoryFeature.BitFlag & 4) = 4; -- Bitwise AND with 4 (0100)
Create SQL query with SqlQueryBuilder 3
var (sql3, parameters3) = new SqlQueryBuilder()
.Select()
.Columns("Categories.CategoryID", "Categories.CategoryName")
.From(new Table("Categories"), new VALUES(new List<List<object>>()
{
new List<object>() {1, 3 },
new List<object>() {2, 5 },
new List<object>() {3, 0 },
}, "CategoryFeature", "CategoryID", "BitFlag"))
.Where(new Where(new Column("Categories.CategoryID").Equale(new Column("CategoryFeature.CategoryID")))
.AND(new ColumnArithmatic().StartBracket("CategoryFeature.BitFlag").BitwiseAND(4).EndBracket().Equale(4))
)
.Build();
Query build by SqlQueryBuilder 3
SELECT Categories.CategoryID,
Categories.CategoryName
FROM Categories, (VALUES (@pMAIN_2606200149202897110, @pMAIN_2606200149202897111), (@pMAIN_2606200149202897112, @pMAIN_2606200149202897113), (@pMAIN_2606200149202897114, @pMAIN_2606200149202897115)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2606200149202897116) = @pMAIN_2606200149202897117;
Parameters (If used)
| Name |
Value |
| @pMAIN_2606200149202897110 |
1 |
| @pMAIN_2606200149202897111 |
3 |
| @pMAIN_2606200149202897112 |
2 |
| @pMAIN_2606200149202897113 |
5 |
| @pMAIN_2606200149202897114 |
3 |
| @pMAIN_2606200149202897115 |
0 |
| @pMAIN_2606200149202897116 |
4 |
| @pMAIN_2606200149202897117 |
4 |
Query Results 3:
| |
CategoryID |
CategoryName |
| 1 |
2
|
Condiments
|
4. Get Categories that are either Active or Featured
SQL Server Query 4
-- Bit 1: 1 = Active, 0 = Inactive
-- Bit 2: 1 = Seasonal, 0 = Non-seasonal
-- Bit 3: 1 = Featured, 0 = Not featured
-- 3. Get Featured Categories
SELECT Categories.CategoryID, Categories.CategoryName
FROM Categories, (VALUES
(1, 3), -- 1 = CategoryID, 3 = BitFlag: Active and Seasonal (1 | 2 = 3)
(2, 5), -- 2 = CategoryID, 5 = BitFlag: Active and Featured (1 | 4 = 5)
(3, 0) -- 3 = CategoryID, 0 = BitFlag: Inactive and Nonseasonal and Not Feature
) AS CategoryFeature (CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID AND (CategoryFeature.BitFlag & (1 | 4 )) > 0; -- Bitwise AND with (1 OR 4) = 5 (0101)
Create SQL query with SqlQueryBuilder 4
var (sql4, parameters4) = new SqlQueryBuilder()
.Select()
.Columns("Categories.CategoryID", "Categories.CategoryName")
.From(new Table("Categories"), new VALUES(new List<List<object>>()
{
new List<object>() {1, 3 },
new List<object>() {2, 5 },
new List<object>() {3, 0 },
}, "CategoryFeature", "CategoryID", "BitFlag"))
.Where(new Where(new Column("Categories.CategoryID").Equale(new Column("CategoryFeature.CategoryID")))
.AND(new ColumnArithmatic().StartBracket("CategoryFeature.BitFlag").BitwiseAND()
.StartBracket().Value(1).BitwiseOR(4).EndBracket()
.EndBracket().GreaterThan(0))
)
.Build();
Query build by SqlQueryBuilder 4
SELECT Categories.CategoryID,
Categories.CategoryName
FROM Categories, (VALUES (@pMAIN_2606200149202925210, @pMAIN_2606200149202925211), (@pMAIN_2606200149202925212, @pMAIN_2606200149202925213), (@pMAIN_2606200149202925214, @pMAIN_2606200149202925215)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & (@pMAIN_2606200149202925216 | @pMAIN_2606200149202925217)) > @pMAIN_2606200149202925218;
Parameters (If used)
| Name |
Value |
| @pMAIN_2606200149202925210 |
1 |
| @pMAIN_2606200149202925211 |
3 |
| @pMAIN_2606200149202925212 |
2 |
| @pMAIN_2606200149202925213 |
5 |
| @pMAIN_2606200149202925214 |
3 |
| @pMAIN_2606200149202925215 |
0 |
| @pMAIN_2606200149202925216 |
1 |
| @pMAIN_2606200149202925217 |
4 |
| @pMAIN_2606200149202925218 |
0 |
Query Results 4:
| |
CategoryID |
CategoryName |
| 1 |
1
|
Beverages
|
| 2 |
2
|
Condiments
|
5. Get Categories that are Active AND Seasonal
SQL Server Query 5
-- Bit 1: 1 = Active, 0 = Inactive
-- Bit 2: 1 = Seasonal, 0 = Non-seasonal
-- Bit 3: 1 = Featured, 0 = Not featured
-- 3. Get Featured Categories
SELECT Categories.CategoryID, Categories.CategoryName
FROM Categories, (VALUES
(1, 3), -- 1 = CategoryID, 3 = BitFlag: Active and Seasonal (1 | 2 = 3)
(2, 5), -- 2 = CategoryID, 5 = BitFlag: Active and Featured (1 | 4 = 5)
(3, 0) -- 3 = CategoryID, 0 = BitFlag: Inactive and Nonseasonal and Not Feature
) AS CategoryFeature (CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID AND (CategoryFeature.BitFlag & (1 | 2 )) = (1 | 2);
Create SQL query with SqlQueryBuilder 5
var (sql5, parameters5) = new SqlQueryBuilder()
.Select()
.Columns("Categories.CategoryID", "Categories.CategoryName")
.From(new Table("Categories"), new VALUES(new List<List<object>>()
{
new List<object>() {1, 3 },
new List<object>() {2, 5 },
new List<object>() {3, 0 },
}, "CategoryFeature", "CategoryID", "BitFlag"))
.Where(new Where(new Column("Categories.CategoryID").Equale(new Column("CategoryFeature.CategoryID")))
.AND(new ColumnArithmatic().StartBracket("CategoryFeature.BitFlag").BitwiseAND()
.StartBracket().Value(1).BitwiseOR(2).EndBracket()
.EndBracket().Equale(new ColumnArithmatic().StartBracket().Value(1).BitwiseOR(2).EndBracket()))
)
.Build();
Query build by SqlQueryBuilder 5
SELECT Categories.CategoryID,
Categories.CategoryName
FROM Categories, (VALUES (@pMAIN_2606200149202956090, @pMAIN_2606200149202956091), (@pMAIN_2606200149202956092, @pMAIN_2606200149202956093), (@pMAIN_2606200149202956094, @pMAIN_2606200149202956095)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & (@pMAIN_2606200149202956096 | @pMAIN_2606200149202956097)) = (@pMAIN_2606200149202956098 | @pMAIN_2606200149202956099);
Parameters (If used)
| Name |
Value |
| @pMAIN_2606200149202956090 |
1 |
| @pMAIN_2606200149202956091 |
3 |
| @pMAIN_2606200149202956092 |
2 |
| @pMAIN_2606200149202956093 |
5 |
| @pMAIN_2606200149202956094 |
3 |
| @pMAIN_2606200149202956095 |
0 |
| @pMAIN_2606200149202956096 |
1 |
| @pMAIN_2606200149202956097 |
2 |
| @pMAIN_2606200149202956098 |
1 |
| @pMAIN_2606200149202956099 |
2 |
Query Results 5:
| |
CategoryID |
CategoryName |
| 1 |
1
|
Beverages
|
6. Get Inactive Categories
SQL Server Query 6
-- Bit 1: 1 = Active, 0 = Inactive
-- Bit 2: 1 = Seasonal, 0 = Non-seasonal
-- Bit 3: 1 = Featured, 0 = Not featured
-- 3. Get Featured Categories
SELECT Categories.CategoryID, Categories.CategoryName
FROM Categories, (VALUES
(1, 3), -- 1 = CategoryID, 3 = BitFlag: Active and Seasonal (1 | 2 = 3)
(2, 5), -- 2 = CategoryID, 5 = BitFlag: Active and Featured (1 | 4 = 5)
(3, 0) -- 3 = CategoryID, 0 = BitFlag: Inactive and Nonseasonal and Not Feature
) AS CategoryFeature (CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID AND (CategoryFeature.BitFlag & 1) = 0;
Create SQL query with SqlQueryBuilder 6
var (sql6, parameters6) = new SqlQueryBuilder()
.Select()
.Columns("Categories.CategoryID", "Categories.CategoryName")
.From(new Table("Categories"), new VALUES(new List<List<object>>()
{
new List<object>() {1, 3 },
new List<object>() {2, 5 },
new List<object>() {3, 0 },
}, "CategoryFeature", "CategoryID", "BitFlag"))
.Where(new Where(new Column("Categories.CategoryID").Equale(new Column("CategoryFeature.CategoryID")))
.AND(new ColumnArithmatic().StartBracket("CategoryFeature.BitFlag").BitwiseAND(1)
.EndBracket().Equale(0))
)
.Build();
Query build by SqlQueryBuilder 6
SELECT Categories.CategoryID,
Categories.CategoryName
FROM Categories, (VALUES (@pMAIN_2606200149202986970, @pMAIN_2606200149202986971), (@pMAIN_2606200149202986972, @pMAIN_2606200149202986973), (@pMAIN_2606200149202986974, @pMAIN_2606200149202986975)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2606200149202986976) = @pMAIN_2606200149202986977;
Parameters (If used)
| Name |
Value |
| @pMAIN_2606200149202986970 |
1 |
| @pMAIN_2606200149202986971 |
3 |
| @pMAIN_2606200149202986972 |
2 |
| @pMAIN_2606200149202986973 |
5 |
| @pMAIN_2606200149202986974 |
3 |
| @pMAIN_2606200149202986975 |
0 |
| @pMAIN_2606200149202986976 |
1 |
| @pMAIN_2606200149202986977 |
0 |
Query Results 6:
| |
CategoryID |
CategoryName |
| 1 |
3
|
Confections
|