Trouble understanding equation involving ampersand

Hello all,

I have little experience with how the ampersand works in the context of SQL Server so I'm hoping someone can help me in that regard.  I have a stored procedure that returns a bit indicator for a status.  Within the sproc I have the following code:

declare @TeamEventEnabled int = 0x1000000;

iif(@EventStartDate > @NewTeamEventEffectiveDate AND @EventType = 0 AND @MyAccountStatus & @TeamEventEnabled > 0, 1, 0);

Open in new window

The other variables are parameters fed into the sproc.  The first two conditions of the IIF statement are straight forward but the last one involving @MyAccountStatus (this is an int) and @TeamEventEnabled puzzles me.  What exactly is it evaluating?

Thanks in advance,
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Nakul VachhrajaniConnect With a Mentor Technical Architect, Capgemini IndiaCommented:
You would basically use bitwise AND in a SQL query to figure out what two values have in common.

Because an integer is actually a collection of bits (little 1s and 0s), This is typically used when you want to represent a combination of switches (1 = on, 0 = off) as a single integer. You can then use a bitwise AND to find out overlaps between two sets of switches.

Here's a small example that I could come up with:

FYI - Pardon my ignorance of your enumeration values, I am just going ahead with
      some samples to demonstrate the concept

Assume that an Account Status represents a total of 4 different values
    - Bit #1: Account is Active
    - Bit #2: Customer has a Debit Card
    - Bit #3: Customer has a bank locker
    - Bit #4: Customer has open service request

Similiarly, a TeamEvent could be a combination of
     - Bit #1: Customer Requested Important notifications to be sent
     - Bit #2: Debit Card Dispatched
     - Bit #3: Bank Locker annual payment due
     - Bit #4: Appointment with representative available
--Now, assume I have 5 customers:
DECLARE @MyAccountStatus1 INT =  8; --1000 - only active customer, no other services availed
DECLARE @MyAccountStatus2 INT = 12; --1100 - Active customer, with debit card
DECLARE @MyAccountStatus3 INT = 14; --1110 - Active customer, with debit card and bank locker
DECLARE @MyAccountStatus4 INT = 10; --1010 - Active customer, without debit card, but with bank locker
DECLARE @MyAccountStatus5 INT =  1; --0001 - New customer, would like to meet with a representative

--Now, assume that we need to send out notifications to all active customers holding a debit card
DECLARE @TeamEventEnabled INT = 12 --1100

--Let's see who will get the notification
SELECT @MyAccountStatus1 & @TeamEventEnabled AS Customer1, --Result = 8, does not have a debit card
       @MyAccountStatus2 & @TeamEventEnabled AS Customer2, --Result = 12, send notification
       @MyAccountStatus3 & @TeamEventEnabled AS Customer3, --Result = 12, send notification
       @MyAccountStatus4 & @TeamEventEnabled AS Customer4, --Result = 8, does not have a debit card
       @MyAccountStatus5 & @TeamEventEnabled AS Customer5; --Result = 0, do not send notification

--Now, let's schedule appointments
SET @TeamEventEnabled = 1;

--Let's see who will get the notification
SELECT @MyAccountStatus1 & @TeamEventEnabled AS Customer1, --Result = 0, do not send notification
       @MyAccountStatus2 & @TeamEventEnabled AS Customer2, --Result = 0, do not send notification
       @MyAccountStatus3 & @TeamEventEnabled AS Customer3, --Result = 0, do not send notification
       @MyAccountStatus4 & @TeamEventEnabled AS Customer4, --Result = 0, do not send notification
       @MyAccountStatus5 & @TeamEventEnabled AS Customer5; --Result > 0, confirm appointment

Open in new window

Pawan KumarConnect With a Mentor Database ExpertCommented:
It is BitWise AND.

It performs a bitwise logical AND operation between two integer values.

The & bitwise operator performs a bitwise logical AND between the two expressions, taking each corresponding bit for both expressions.

SQL Server will internally convert the parameters into Bits ( binary format and then apply the operation ), Example Below

SELECT 7 & 2

0000 0000 0000 0111  -- 7
0000 0000 0000 0010  -- 2
0000 0000 0000 0010  -- Only the 2 bit is common in both 7 and 2

For each single digit if the value is 1 then we get 1 else 0

0000 0000 0000 0111
0000 0000 0000 0010

for 2nd position from right we have 1 & 1 , so if value are 1 & 1 then we get 1.

For other 1 & 0 , 0 & 0 , 0 & 1 --- we will get 0.

Below is how the binary equivalent is calculated.

For 7 -- its internal binary representation is 0000 0000 0000 0111.  Divide the number by 2 take reminder until your get 0.

7 /  2  ---- 1
3 / 2 ------- 1
1/ 2  ------- 1

All remaining will be zero since we have to make it 16 digits.  <<16 digits Binary System>>
So 0000 0000 0000 0111

Source -

Hope it helps
jisoo411Author Commented:
Thank you both for explaining it to me, I think I understand the basic functionality.  It's basically comparing two integers at a binary level.  I think the only follow up question would be why do this in the first place?  It looks like that condition is just comparing two integers, so why the need to examine it at such a detailed level?
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
It all depends on the original design. Rather than storing 4 related bit fields in 4 different columns, designers may want to combine it into a single integer.

It's more of a design decision that the application designers would have taken to accommodate multiple related bit fields into a single field/value so that rather than exchanging multiple values, you only need to work with one value.
jisoo411Author Commented:
Thank you both for your help with understanding this concept!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.