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_2606220324494865700, @pMAIN_2606220324494865701), (@pMAIN_2606220324494865702, @pMAIN_2606220324494865703), (@pMAIN_2606220324494865704, @pMAIN_2606220324494865705)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2606220324494865706) = @pMAIN_2606220324494865707;
Parameters (If used)
| Name |
Value |
| @pMAIN_2606220324494865700 |
1 |
| @pMAIN_2606220324494865701 |
3 |
| @pMAIN_2606220324494865702 |
2 |
| @pMAIN_2606220324494865703 |
5 |
| @pMAIN_2606220324494865704 |
3 |
| @pMAIN_2606220324494865705 |
0 |
| @pMAIN_2606220324494865706 |
1 |
| @pMAIN_2606220324494865707 |
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_2606220324494897010, @pMAIN_2606220324494897011), (@pMAIN_2606220324494897012, @pMAIN_2606220324494897013), (@pMAIN_2606220324494897014, @pMAIN_2606220324494897015)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2606220324494897016) = @pMAIN_2606220324494897017;
Parameters (If used)
| Name |
Value |
| @pMAIN_2606220324494897010 |
1 |
| @pMAIN_2606220324494897011 |
3 |
| @pMAIN_2606220324494897012 |
2 |
| @pMAIN_2606220324494897013 |
5 |
| @pMAIN_2606220324494897014 |
3 |
| @pMAIN_2606220324494897015 |
0 |
| @pMAIN_2606220324494897016 |
2 |
| @pMAIN_2606220324494897017 |
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_2606220324494917910, @pMAIN_2606220324494917911), (@pMAIN_2606220324494917912, @pMAIN_2606220324494917913), (@pMAIN_2606220324494917914, @pMAIN_2606220324494917915)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2606220324494917916) = @pMAIN_2606220324494917917;
Parameters (If used)
| Name |
Value |
| @pMAIN_2606220324494917910 |
1 |
| @pMAIN_2606220324494917911 |
3 |
| @pMAIN_2606220324494917912 |
2 |
| @pMAIN_2606220324494917913 |
5 |
| @pMAIN_2606220324494917914 |
3 |
| @pMAIN_2606220324494917915 |
0 |
| @pMAIN_2606220324494917916 |
4 |
| @pMAIN_2606220324494917917 |
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_2606220324494939590, @pMAIN_2606220324494939591), (@pMAIN_2606220324494939592, @pMAIN_2606220324494939593), (@pMAIN_2606220324494939594, @pMAIN_2606220324494939595)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & (@pMAIN_2606220324494939596 | @pMAIN_2606220324494939597)) > @pMAIN_2606220324494939598;
Parameters (If used)
| Name |
Value |
| @pMAIN_2606220324494939590 |
1 |
| @pMAIN_2606220324494939591 |
3 |
| @pMAIN_2606220324494939592 |
2 |
| @pMAIN_2606220324494939593 |
5 |
| @pMAIN_2606220324494939594 |
3 |
| @pMAIN_2606220324494939595 |
0 |
| @pMAIN_2606220324494939596 |
1 |
| @pMAIN_2606220324494939597 |
4 |
| @pMAIN_2606220324494939598 |
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_2606220324494961090, @pMAIN_2606220324494961091), (@pMAIN_2606220324494961092, @pMAIN_2606220324494961093), (@pMAIN_2606220324494961094, @pMAIN_2606220324494961095)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & (@pMAIN_2606220324494961096 | @pMAIN_2606220324494961097)) = (@pMAIN_2606220324494961098 | @pMAIN_2606220324494961099);
Parameters (If used)
| Name |
Value |
| @pMAIN_2606220324494961090 |
1 |
| @pMAIN_2606220324494961091 |
3 |
| @pMAIN_2606220324494961092 |
2 |
| @pMAIN_2606220324494961093 |
5 |
| @pMAIN_2606220324494961094 |
3 |
| @pMAIN_2606220324494961095 |
0 |
| @pMAIN_2606220324494961096 |
1 |
| @pMAIN_2606220324494961097 |
2 |
| @pMAIN_2606220324494961098 |
1 |
| @pMAIN_2606220324494961099 |
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_2606220324494984180, @pMAIN_2606220324494984181), (@pMAIN_2606220324494984182, @pMAIN_2606220324494984183), (@pMAIN_2606220324494984184, @pMAIN_2606220324494984185)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2606220324494984186) = @pMAIN_2606220324494984187;
Parameters (If used)
| Name |
Value |
| @pMAIN_2606220324494984180 |
1 |
| @pMAIN_2606220324494984181 |
3 |
| @pMAIN_2606220324494984182 |
2 |
| @pMAIN_2606220324494984183 |
5 |
| @pMAIN_2606220324494984184 |
3 |
| @pMAIN_2606220324494984185 |
0 |
| @pMAIN_2606220324494984186 |
1 |
| @pMAIN_2606220324494984187 |
0 |
Query Results 6:
| |
CategoryID |
CategoryName |
| 1 |
3
|
Confections
|