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_2509042318065759990, @pMAIN_2509042318065759991), (@pMAIN_2509042318065759992, @pMAIN_2509042318065759993), (@pMAIN_2509042318065759994, @pMAIN_2509042318065759995)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2509042318065759996) = @pMAIN_2509042318065759997;
Parameters (If used)
Name |
Value |
@pMAIN_2509042318065759990 |
1 |
@pMAIN_2509042318065759991 |
3 |
@pMAIN_2509042318065759992 |
2 |
@pMAIN_2509042318065759993 |
5 |
@pMAIN_2509042318065759994 |
3 |
@pMAIN_2509042318065759995 |
0 |
@pMAIN_2509042318065759996 |
1 |
@pMAIN_2509042318065759997 |
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_2509042318065847480, @pMAIN_2509042318065847481), (@pMAIN_2509042318065847482, @pMAIN_2509042318065847483), (@pMAIN_2509042318065847484, @pMAIN_2509042318065847485)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2509042318065847486) = @pMAIN_2509042318065847487;
Parameters (If used)
Name |
Value |
@pMAIN_2509042318065847480 |
1 |
@pMAIN_2509042318065847481 |
3 |
@pMAIN_2509042318065847482 |
2 |
@pMAIN_2509042318065847483 |
5 |
@pMAIN_2509042318065847484 |
3 |
@pMAIN_2509042318065847485 |
0 |
@pMAIN_2509042318065847486 |
2 |
@pMAIN_2509042318065847487 |
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_2509042318065888440, @pMAIN_2509042318065888441), (@pMAIN_2509042318065888442, @pMAIN_2509042318065888443), (@pMAIN_2509042318065888444, @pMAIN_2509042318065888445)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2509042318065888446) = @pMAIN_2509042318065888447;
Parameters (If used)
Name |
Value |
@pMAIN_2509042318065888440 |
1 |
@pMAIN_2509042318065888441 |
3 |
@pMAIN_2509042318065888442 |
2 |
@pMAIN_2509042318065888443 |
5 |
@pMAIN_2509042318065888444 |
3 |
@pMAIN_2509042318065888445 |
0 |
@pMAIN_2509042318065888446 |
4 |
@pMAIN_2509042318065888447 |
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_2509042318065917480, @pMAIN_2509042318065917481), (@pMAIN_2509042318065917482, @pMAIN_2509042318065917483), (@pMAIN_2509042318065917484, @pMAIN_2509042318065917485)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & (@pMAIN_2509042318065917486 | @pMAIN_2509042318065917487)) > @pMAIN_2509042318065917488;
Parameters (If used)
Name |
Value |
@pMAIN_2509042318065917480 |
1 |
@pMAIN_2509042318065917481 |
3 |
@pMAIN_2509042318065917482 |
2 |
@pMAIN_2509042318065917483 |
5 |
@pMAIN_2509042318065917484 |
3 |
@pMAIN_2509042318065917485 |
0 |
@pMAIN_2509042318065917486 |
1 |
@pMAIN_2509042318065917487 |
4 |
@pMAIN_2509042318065917488 |
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_2509042318065948030, @pMAIN_2509042318065948031), (@pMAIN_2509042318065948032, @pMAIN_2509042318065948033), (@pMAIN_2509042318065948034, @pMAIN_2509042318065948035)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & (@pMAIN_2509042318065948036 | @pMAIN_2509042318065948037)) = (@pMAIN_2509042318065948038 | @pMAIN_2509042318065948039);
Parameters (If used)
Name |
Value |
@pMAIN_2509042318065948030 |
1 |
@pMAIN_2509042318065948031 |
3 |
@pMAIN_2509042318065948032 |
2 |
@pMAIN_2509042318065948033 |
5 |
@pMAIN_2509042318065948034 |
3 |
@pMAIN_2509042318065948035 |
0 |
@pMAIN_2509042318065948036 |
1 |
@pMAIN_2509042318065948037 |
2 |
@pMAIN_2509042318065948038 |
1 |
@pMAIN_2509042318065948039 |
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_2509042318065977790, @pMAIN_2509042318065977791), (@pMAIN_2509042318065977792, @pMAIN_2509042318065977793), (@pMAIN_2509042318065977794, @pMAIN_2509042318065977795)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2509042318065977796) = @pMAIN_2509042318065977797;
Parameters (If used)
Name |
Value |
@pMAIN_2509042318065977790 |
1 |
@pMAIN_2509042318065977791 |
3 |
@pMAIN_2509042318065977792 |
2 |
@pMAIN_2509042318065977793 |
5 |
@pMAIN_2509042318065977794 |
3 |
@pMAIN_2509042318065977795 |
0 |
@pMAIN_2509042318065977796 |
1 |
@pMAIN_2509042318065977797 |
0 |
Query Results 6:
|
CategoryID |
CategoryName |
1 |
3
|
Confections
|