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_2507200126446929970, @pMAIN_2507200126446929971), (@pMAIN_2507200126446929972, @pMAIN_2507200126446929973), (@pMAIN_2507200126446929974, @pMAIN_2507200126446929975)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2507200126446929976) = @pMAIN_2507200126446929977;
Parameters (If used)
Name |
Value |
@pMAIN_2507200126446929970 |
1 |
@pMAIN_2507200126446929971 |
3 |
@pMAIN_2507200126446929972 |
2 |
@pMAIN_2507200126446929973 |
5 |
@pMAIN_2507200126446929974 |
3 |
@pMAIN_2507200126446929975 |
0 |
@pMAIN_2507200126446929976 |
1 |
@pMAIN_2507200126446929977 |
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_2507200126447574070, @pMAIN_2507200126447574071), (@pMAIN_2507200126447574072, @pMAIN_2507200126447574073), (@pMAIN_2507200126447574074, @pMAIN_2507200126447574075)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2507200126447574076) = @pMAIN_2507200126447574077;
Parameters (If used)
Name |
Value |
@pMAIN_2507200126447574070 |
1 |
@pMAIN_2507200126447574071 |
3 |
@pMAIN_2507200126447574072 |
2 |
@pMAIN_2507200126447574073 |
5 |
@pMAIN_2507200126447574074 |
3 |
@pMAIN_2507200126447574075 |
0 |
@pMAIN_2507200126447574076 |
2 |
@pMAIN_2507200126447574077 |
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_2507200126449464250, @pMAIN_2507200126449464251), (@pMAIN_2507200126449464252, @pMAIN_2507200126449464253), (@pMAIN_2507200126449464254, @pMAIN_2507200126449464255)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2507200126449464256) = @pMAIN_2507200126449464257;
Parameters (If used)
Name |
Value |
@pMAIN_2507200126449464250 |
1 |
@pMAIN_2507200126449464251 |
3 |
@pMAIN_2507200126449464252 |
2 |
@pMAIN_2507200126449464253 |
5 |
@pMAIN_2507200126449464254 |
3 |
@pMAIN_2507200126449464255 |
0 |
@pMAIN_2507200126449464256 |
4 |
@pMAIN_2507200126449464257 |
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_2507200126449512590, @pMAIN_2507200126449512591), (@pMAIN_2507200126449512592, @pMAIN_2507200126449512593), (@pMAIN_2507200126449512594, @pMAIN_2507200126449512595)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & (@pMAIN_2507200126449512596 | @pMAIN_2507200126449512597)) > @pMAIN_2507200126449512598;
Parameters (If used)
Name |
Value |
@pMAIN_2507200126449512590 |
1 |
@pMAIN_2507200126449512591 |
3 |
@pMAIN_2507200126449512592 |
2 |
@pMAIN_2507200126449512593 |
5 |
@pMAIN_2507200126449512594 |
3 |
@pMAIN_2507200126449512595 |
0 |
@pMAIN_2507200126449512596 |
1 |
@pMAIN_2507200126449512597 |
4 |
@pMAIN_2507200126449512598 |
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_2507200126449572610, @pMAIN_2507200126449572611), (@pMAIN_2507200126449572612, @pMAIN_2507200126449572613), (@pMAIN_2507200126449572614, @pMAIN_2507200126449572615)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & (@pMAIN_2507200126449572616 | @pMAIN_2507200126449572617)) = (@pMAIN_2507200126449572618 | @pMAIN_2507200126449572619);
Parameters (If used)
Name |
Value |
@pMAIN_2507200126449572610 |
1 |
@pMAIN_2507200126449572611 |
3 |
@pMAIN_2507200126449572612 |
2 |
@pMAIN_2507200126449572613 |
5 |
@pMAIN_2507200126449572614 |
3 |
@pMAIN_2507200126449572615 |
0 |
@pMAIN_2507200126449572616 |
1 |
@pMAIN_2507200126449572617 |
2 |
@pMAIN_2507200126449572618 |
1 |
@pMAIN_2507200126449572619 |
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_2507200126449627630, @pMAIN_2507200126449627631), (@pMAIN_2507200126449627632, @pMAIN_2507200126449627633), (@pMAIN_2507200126449627634, @pMAIN_2507200126449627635)) AS CategoryFeature(CategoryID, BitFlag)
WHERE Categories.CategoryID = CategoryFeature.CategoryID
AND (CategoryFeature.BitFlag & @pMAIN_2507200126449627636) = @pMAIN_2507200126449627637;
Parameters (If used)
Name |
Value |
@pMAIN_2507200126449627630 |
1 |
@pMAIN_2507200126449627631 |
3 |
@pMAIN_2507200126449627632 |
2 |
@pMAIN_2507200126449627633 |
5 |
@pMAIN_2507200126449627634 |
3 |
@pMAIN_2507200126449627635 |
0 |
@pMAIN_2507200126449627636 |
1 |
@pMAIN_2507200126449627637 |
0 |
Query Results 6:
|
CategoryID |
CategoryName |
1 |
3
|
Confections
|