<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Fool &#187; operators</title>
	<atom:link href="http://sqlfool.com/tag/operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlfool.com</link>
	<description>Self-Professed SQL Scripting Junkie!</description>
	<lastBuildDate>Wed, 02 May 2012 21:25:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>T-SQL Bitwise Operations</title>
		<link>http://sqlfool.com/2009/02/bitwise-operations/</link>
		<comments>http://sqlfool.com/2009/02/bitwise-operations/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 14:38:46 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[operators]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=435</guid>
		<description><![CDATA[I&#8217;ve seen bit-product columns from time-to-time, mostly in SQL Server 2000 system tables, but it&#8217;s never been something I&#8217;ve had to work with. And when I&#8217;ve needed to, I&#8217;ve known how to figure out which options are selected, i.e. a bit product of 9 means options 8 and 1 are selected. If you&#8217;ve ever taken [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve seen bit-product columns from time-to-time, mostly in SQL Server 2000 system tables, but it&#8217;s never been something I&#8217;ve had to work with.  And when I&#8217;ve needed to, I&#8217;ve known how to figure out which options are selected, i.e. a bit product of 9 means options 8 and 1 are selected.  If you&#8217;ve ever taken a look at the [status] column on the <a href="http://msdn.microsoft.com/en-us/library/aa260406(SQL.80).aspx" target="_blank">sysdatabases</a> table (SQL 2000), you&#8217;ll know what I&#8217;m talking about.</p>
<p>What I&#8217;ve never known how to do, until recently, was calculate these options programmatically.  That&#8217;s why, when I noticed the [freq_interval] on the <a href="http://msdn.microsoft.com/en-us/library/ms178644.aspx" target="_blank">sysschedules</a> table was a bit-product column, I decided to spend a little time figuring it out.  Fortunately for me, a couple of my awesome co-workers, Jeff M. and Jason H., have worked with this before and were able to explain it to me.  And, it turns out, it&#8217;s actually quite easy.</p>
<p>Let me back up a few steps in case you&#8217;re not familiar with this topic.  If you check out the Books Online entry for the <a href="http://msdn.microsoft.com/en-us/library/ms178644.aspx" target="_blank">sysschedules</a> table (2005), you&#8217;ll notice the following statement:</p>
<blockquote><p>
<strong>freq_interval</strong> is one or more of the following:<br />
1 = Sunday<br />
2 = Monday<br />
4 = Tuesday<br />
8 = Wednesday<br />
16 = Thursday<br />
32 = Friday<br />
64 = Saturday</p></blockquote>
<p>When I looked at the actual value in the table, the schedule has a [freq_interval] value of 42, which is the sum of the bit values for the days selected.  </p>
<p>If there were more than 7 options, the bit values would continue to double, i.e. 128, 256, etc.  And regardless of how many bit values you select, you&#8217;re guaranteed one and only one possible answer, as the sum of all previous bit values will never exceed the next bit value:<br />
1 + 2 = 3<br />
1 + 2 + 4 = 7<br />
1 + 2 + 4 + 8 = 15</p>
<p>Knowing this, I&#8217;m able to retrieve the values manually: I start with the highest bit value that does not exceed 42, then subtract it; I repeat until I&#8217;m left with 0.</p>
<p>So&#8230;<br />
42 &#8211; 32 = 10<br />
10 &#8211; 8 = 2<br />
2 &#8211; 2 = 0</p>
<p>That means my job is scheduled to run on Friday&#8217;s (32), Wednesday&#8217;s (8), and Monday&#8217;s (2).  </p>
<p>Now how do I do this with T-SQL?  SQL Server provides an operator specifically for this task: the <a href="http://msdn.microsoft.com/en-us/library/ms174965.aspx" target="_blank">bitwise AND</a> operator (&#038;).  <em>For now, I&#8217;m going to skip the &#8220;why&#8221; behind this and just get to the practical application.  If you&#8217;re interested in the &#8220;why,&#8221; let me know and I&#8217;ll write a follow-up post on binary and logical AND and OR operations.</em></p>
<p>For example, to use the bitwise AND to find out which days are selected&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Select</span> <span style="color: #000;">42</span> <span style="color: #808080;">&amp;</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'Sunday'</span>
    , <span style="color: #000;">42</span> <span style="color: #808080;">&amp;</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'Monday'</span>
    , <span style="color: #000;">42</span> <span style="color: #808080;">&amp;</span> <span style="color: #000;">4</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'Tuesday'</span>
    , <span style="color: #000;">42</span> <span style="color: #808080;">&amp;</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'Wednesday'</span>
    , <span style="color: #000;">42</span> <span style="color: #808080;">&amp;</span> <span style="color: #000;">16</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'Thursday'</span>
    , <span style="color: #000;">42</span> <span style="color: #808080;">&amp;</span> <span style="color: #000;">32</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'Friday'</span>
    , <span style="color: #000;">42</span> <span style="color: #808080;">&amp;</span> <span style="color: #000;">64</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'Saturday'</span>;</pre></div></div>

<p>&#8230; will return &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Sunday      Monday      Tuesday     Wednesday   Thursday    Friday      Saturday
----------- ----------- ----------- ----------- ----------- ----------- -----------
0           2           0           8           0           32          0</pre></div></div>

<p>If the result is not equal to zero, then that day is selected.  Easy as key lime pie, right?</p>
<p>Now let&#8217;s take it a step further and create our own working example.  Let&#8217;s say we&#8217;re going to track the characteristics of various objects in a single bit-product column (<em>note: this is not necessarily the best way to accomplish this in the real world, but it&#8217;s a good illustration</em>).  First, set up a table to use in our example.  This table will have a column, [attributes], which will hold the sum of our bit values.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> myTable
<span style="color: #808080;">&#40;</span>
      id            <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">identity</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
    , item          <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>
    , attributes    <span style="color: #0000FF;">int</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> myTable
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'Broccoli'</span>, <span style="color: #000;">200</span> <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'Tomato'</span>, <span style="color: #000;">193</span> <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'Car'</span>, <span style="color: #000;">276</span> <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #FF0000;">'Ball'</span>, <span style="color: #000;">292</span>;</pre></div></div>

<p>Next, we&#8217;re going to create a table variable that holds characteristics and their values.  We&#8217;ll then join these two tables together to see which attributes exist for each item.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Declare</span> @statusLookup <span style="color: #0000FF;">Table</span>
<span style="color: #808080;">&#40;</span>
      attribute <span style="color: #0000FF;">int</span>
    , <span style="color: #0000FF;">value</span>     <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> @statusLookup
<span style="color: #0000FF;">Select</span> <span style="color: #000;">1</span>, <span style="color: #FF0000;">'Red'</span> <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">4</span>, <span style="color: #FF0000;">'Blue'</span> <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">8</span>, <span style="color: #FF0000;">'Green'</span> <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">16</span>, <span style="color: #FF0000;">'Metal'</span> <span style="color: #0000FF;">Union</span> All
&nbsp;
<span style="color: #0000FF;">Select</span> <span style="color: #000;">32</span>, <span style="color: #FF0000;">'Plastic'</span> <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">64</span>, <span style="color: #FF0000;">'Plant'</span> <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">128</span>, <span style="color: #FF0000;">'Edible'</span> <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">256</span>, <span style="color: #FF0000;">'Non-Edible'</span>;
&nbsp;
<span style="color: #0000FF;">Select</span> a.<span style="color: #202020;">item</span>, b.<span style="color: #0000FF;">value</span>
<span style="color: #0000FF;">From</span> myTable a
Cross Join @statusLookup b
<span style="color: #0000FF;">Where</span> a.<span style="color: #202020;">attributes</span> <span style="color: #808080;">&amp;</span> b.<span style="color: #202020;">attribute</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span>
<span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> a.<span style="color: #202020;">item</span>
    , b.<span style="color: #0000FF;">value</span></pre></div></div>

<p>You should get this result:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">item       value
---------- ----------
Ball       Blue
Ball       Non-Edible
Ball       Plastic
Broccoli   Edible
Broccoli   Green
Broccoli   Plant
Car        Blue
Car        Metal
Car        Non-Edible
Tomato     Edible
Tomato     Plant
Tomato     Red</pre></div></div>

<p>Great, now we know broccoli is edible!  Let&#8217;s apply a little XML to clean up the results&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Select</span> a.<span style="color: #202020;">item</span>
    , <span style="color: #FF00FF;">Replace</span><span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">Replace</span><span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">Replace</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span> 
        <span style="color: #0000FF;">Select</span> <span style="color: #0000FF;">value</span> 
        <span style="color: #0000FF;">From</span> @statusLookup <span style="color: #0000FF;">AS</span> b 
        <span style="color: #0000FF;">Where</span> a.<span style="color: #202020;">attributes</span> <span style="color: #808080;">&amp;</span> b.<span style="color: #202020;">attribute</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
        <span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> b.<span style="color: #0000FF;">value</span> <span style="color: #0000FF;">For</span> XML Raw<span style="color: #808080;">&#41;</span>
        , <span style="color: #FF0000;">'&quot;/&gt;&lt;row value=&quot;'</span>, <span style="color: #FF0000;">', '</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'&lt;row value=&quot;'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'&quot;/&gt;'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> 
        <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'attributes'</span>
<span style="color: #0000FF;">From</span> myTable a
<span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> a.<span style="color: #202020;">item</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">item       attributes
----------------------------------------
Ball       Blue, Non-Edible, Plastic
Broccoli   Edible, Green, Plant
Car        Blue, Metal, Non-Edible
Tomato     Edible, Plant, Red</pre></div></div>

<p>Voila!  There you have it, how to use the bitwise AND (&#038;) operator to retrieve multiple values from a bit-product column.  Pretty neat stuff!</p>
<p>Special thanks to Jeff M. and Jason H. for their assistance.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Happy Coding!</p>
<p>Michelle Ufford (aka <a href="http://sqlfool.com">SQLFool</a>)</p>
<p>Source: <a href="http://sqlfool.com/2009/02/bitwise-operations/">http://sqlfool.com/2009/02/bitwise-operations/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2009/02/bitwise-operations/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

