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_2602032108459387800, @pMAIN_2602032108459387801), (@pMAIN_2602032108459387802, @pMAIN_2602032108459387803), (@pMAIN_2602032108459387804, @pMAIN_2602032108459387805)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2602032108459387806) = @pMAIN_2602032108459387807;
Parameters (If used)
| Name |
Value |
| @pMAIN_2602032108459387800 |
1 |
| @pMAIN_2602032108459387801 |
3 |
| @pMAIN_2602032108459387802 |
2 |
| @pMAIN_2602032108459387803 |
5 |
| @pMAIN_2602032108459387804 |
3 |
| @pMAIN_2602032108459387805 |
0 |
| @pMAIN_2602032108459387806 |
1 |
| @pMAIN_2602032108459387807 |
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_2602032108459432050, @pMAIN_2602032108459432051), (@pMAIN_2602032108459432052, @pMAIN_2602032108459432053), (@pMAIN_2602032108459432054, @pMAIN_2602032108459432055)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2602032108459432056) = @pMAIN_2602032108459432057;
Parameters (If used)
| Name |
Value |
| @pMAIN_2602032108459432050 |
1 |
| @pMAIN_2602032108459432051 |
3 |
| @pMAIN_2602032108459432052 |
2 |
| @pMAIN_2602032108459432053 |
5 |
| @pMAIN_2602032108459432054 |
3 |
| @pMAIN_2602032108459432055 |
0 |
| @pMAIN_2602032108459432056 |
2 |
| @pMAIN_2602032108459432057 |
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_2602032108459471680, @pMAIN_2602032108459471681), (@pMAIN_2602032108459471682, @pMAIN_2602032108459471683), (@pMAIN_2602032108459471684, @pMAIN_2602032108459471685)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2602032108459471686) = @pMAIN_2602032108459471687;
Parameters (If used)
| Name |
Value |
| @pMAIN_2602032108459471680 |
1 |
| @pMAIN_2602032108459471681 |
3 |
| @pMAIN_2602032108459471682 |
2 |
| @pMAIN_2602032108459471683 |
5 |
| @pMAIN_2602032108459471684 |
3 |
| @pMAIN_2602032108459471685 |
0 |
| @pMAIN_2602032108459471686 |
4 |
| @pMAIN_2602032108459471687 |
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_2602032108459496260, @pMAIN_2602032108459496261), (@pMAIN_2602032108459496262, @pMAIN_2602032108459496263), (@pMAIN_2602032108459496264, @pMAIN_2602032108459496265)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & (@pMAIN_2602032108459496266 | @pMAIN_2602032108459496267)) > @pMAIN_2602032108459496268;
Parameters (If used)
| Name |
Value |
| @pMAIN_2602032108459496260 |
1 |
| @pMAIN_2602032108459496261 |
3 |
| @pMAIN_2602032108459496262 |
2 |
| @pMAIN_2602032108459496263 |
5 |
| @pMAIN_2602032108459496264 |
3 |
| @pMAIN_2602032108459496265 |
0 |
| @pMAIN_2602032108459496266 |
1 |
| @pMAIN_2602032108459496267 |
4 |
| @pMAIN_2602032108459496268 |
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_2602032108459538680, @pMAIN_2602032108459538681), (@pMAIN_2602032108459538682, @pMAIN_2602032108459538683), (@pMAIN_2602032108459538684, @pMAIN_2602032108459538685)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & (@pMAIN_2602032108459538686 | @pMAIN_2602032108459538687)) = (@pMAIN_2602032108459538688 | @pMAIN_2602032108459538689);
Parameters (If used)
| Name |
Value |
| @pMAIN_2602032108459538680 |
1 |
| @pMAIN_2602032108459538681 |
3 |
| @pMAIN_2602032108459538682 |
2 |
| @pMAIN_2602032108459538683 |
5 |
| @pMAIN_2602032108459538684 |
3 |
| @pMAIN_2602032108459538685 |
0 |
| @pMAIN_2602032108459538686 |
1 |
| @pMAIN_2602032108459538687 |
2 |
| @pMAIN_2602032108459538688 |
1 |
| @pMAIN_2602032108459538689 |
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_2602032108459573950, @pMAIN_2602032108459573951), (@pMAIN_2602032108459573952, @pMAIN_2602032108459573953), (@pMAIN_2602032108459573954, @pMAIN_2602032108459573955)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2602032108459573956) = @pMAIN_2602032108459573957;
Parameters (If used)
| Name |
Value |
| @pMAIN_2602032108459573950 |
1 |
| @pMAIN_2602032108459573951 |
3 |
| @pMAIN_2602032108459573952 |
2 |
| @pMAIN_2602032108459573953 |
5 |
| @pMAIN_2602032108459573954 |
3 |
| @pMAIN_2602032108459573955 |
0 |
| @pMAIN_2602032108459573956 |
1 |
| @pMAIN_2602032108459573957 |
0 |
Query Results 6:
| |
CategoryID |
CategoryName |
| 1 |
3
|
Confections
|