Learn more about SQL Server tools

mssqltips logo

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories


SQL Server Bitwise operators store multiple values in one column

By:   |   Read Comments (5)   |   Related Tips: More > T-SQL

Attend this free live MSSQLTips webcast

Tips and Tricks to Growing Your SQL Server Estate from SMB to Enterprise Scale
Thursday, June 28, 2018 - click here to learn more

Sometimes there may be the need to save multiple values into one column instead of creating multiple columns to store these indicators.  Let's take for example we need to know who has different telephone types and instead of having multiple indicator columns for each type of telephone number you use one column to store the different values.  So for example value 1 = home phone, 2 = mobile phone, 3 = fax, etc...  You could have three different bit (yes/no) columns such as homeNumberIndicator, mobileNumberIndicator and faxNumberIndicator, but each time you add a new phone type you need to add a new column to your table.  Another way to do this is to store the data in a binary format and use the bitwise (&) operator that SQL Server offers to store all of the data in one column and to also easily tell what values are applicable based on the value stored.

Basically what we want to do is store one value for different types of phone numbers that a person has in one column instead of having multiple columns for each different type.  This is just one purpose for doing this, but there are countless other reasons you may want to store data this way. So in order to do this we need to save the data in a binary format, so we can then use the bitwise (&) operator to retrieve the data.  So the first thing we need to do is figure out the values we want to store and then use a binary representation for each of these values.  Here is the representation of the data for each of the different phone types we will store.

  • 2 = home phone number
  • 4 = home fax number
  • 8 = mobile phone number
  • 16 = office phone number
  • 32 = office fax number
  • 64 = toll free office number
  • 128 = toll free office fax number

To better understand how this will work, let's say someone has both a home number and an office number.  The value that would be stored in the data column would be 18 (2 home + 16 office).  Another example could be home number and home fax number which would be 6 (2 home + 4 home fax).

The use of the bitwise operator comes in play when updating and reading data from this column.  What the bitwise operator allows you to do is to compare two different values at a binary level and tell you whether the two numbers intersect. 

Here is a simple example of this operator at work.  We are doing a simple comparison of the first value against the second value.  Since the numbers are based on binary representation, the numbers increase as follows: 1,2,4,8,16, etc...  So for these numbers we want to see where value one intersects with value two.  Here are the examples:

PRINT --(=0)
PRINT --(=1)
PRINT --(=0)
PRINT --(=1)
PRINT --(=0)
PRINT --(=1)
  • For line 1, 1 is not in 0 (0 = 0), so this returns 0.
  • For line 2, 1 is in 1 (0,1 = 1), so this returns 1.
  • For line 3, 1 is not in 2 (0,2 = 2), so this returns 0.
  • For line 4, 1 is in 3 (0,1,2 = 3), so this returns 1.
  • For line 5, 1 is not in 4 (0,4 = 4), so this returns 0.
  • For line 6, 1 is in 5 (0,1,4 = 5), so this returns 1.

To take this a step further and create a representation for our telephone number problem, the following code is a simple set of code that sets the indicator either on=1 or off=0 for each of the components of the phone number indicator.  In addition, to setting the indicators for each value to either 1 or 0, we are also using the power indicator to allow us to get the binary representation easier. The first few lines set the values and prints the binary representation and the second set prints out each line that is true based on the binary representation.

DECLARE @phoneIndicator INT

@home bit
@homeFax bit,
@mobile bit
@office bit
@officeFax bit,
@tollfreeOffice bit
@tollfreeFax bit

SET @home 0
SET @homeFax 0  
SET @mobile 0   
SET @office 0   
SET @officeFax 0        
SET @tollfreeOffice 
SET @tollfreeFax 0      

SET @phoneIndicator POWER(2*@home,1

PRINT @phoneIndicator

IF ( (@phoneIndicatorPRINT 'Has Home'
IF ( (@phoneIndicatorPRINT 'Has Home Fax'
IF ( (@phoneIndicatorPRINT 'Has Mobile'
IF ( (16 @phoneIndicator16 PRINT 'Has Office'
IF ( (32 @phoneIndicator32 PRINT 'Has Office Fax'
IF ( (64 @phoneIndicator64 PRINT 'Has Toll Free Office'
IF ( (128 @phoneIndicator128 PRINT 'Has Toll Free Fax'

Here are some example runs:

Example 1
SET @home 1
SET @homeFax 0  
SET @mobile 0   
SET @office 1   
SET @officeFax 0        
SET @tollfreeOffice 
SET @tollfreeFax 
Example 1 Results
Has Home
Has Office
Example 2
SET @home 1
SET @homeFax 0  
SET @mobile 0   
SET @office 1   
SET @officeFax 0        
SET @tollfreeOffice 
SET @tollfreeFax 
Example 2 Results
Has Home
Has Office
Has Toll Free Fax
Example 3
SET @home 1
SET @homeFax 1  
SET @mobile 1   
SET @office 1   
SET @officeFax 1        
SET @tollfreeOffice 
SET @tollfreeFax 1
Example 3 Results
Has Home
Has Home Fax
Has Mobile
Has Office
Has Office Fax
Has Toll Free Office
Has Toll Free Fax

As you can see the use of the bitwise (&) operator allows us to compare the values to see if there are any intersecting values in the number.  This may not be a perfect solution to your data storage needs, but it does give you another option for storing and retrieving your data.

Next Steps

  • See if this type of processing and data storage makes sense for your database
  • Check out these other bitwise operators

Last Update:

next webcast button

next tip button

About the author
MSSQLTips author Greg Robidoux Greg Robidoux is the President of Edgewood Solutions and a co-founder of MSSQLTips.com.

View all my tips

Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Email me updates 

Signup for our newsletter
 I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.


Wednesday, May 18, 2016 - 10:39:19 AM - Greg Robidoux Back To Top

Hi Manoj,

if you are going to store 100 different options in one column the number will get very large and this might not be the best approach.

If you have 100 separate columns and each one could store up to 10 different options that can be done.


Wednesday, May 18, 2016 - 2:51:46 AM - Manoj Tyagi Back To Top

 If i have 100+ categories and i want to bind the each item with 10+ categories  , is it advisable to store the Catefory ids as Binary  ?

it will increase  as  2,4,8,16................................a very Big number.  Will its be ok to have this for more than 100 categories  ?



Wednesday, July 31, 2013 - 7:35:16 AM - Max Back To Top

Thanks Greg, at last a webpage that explains bitwise logic in a way that I can understand!!! Much appreicated.


Tuesday, March 26, 2013 - 3:21:04 PM - Armando Prato Back To Top

Thanks, Greg!  I needed to do something like this for something I'm working on and this was invaluable!

Tuesday, January 22, 2013 - 3:48:39 AM - O Back To Top

What are the performance considerations?

Learn more about SQL Server tools