<?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>Ramblings of a Crafty DBA</title>
	<atom:link href="http://craftydba.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://craftydba.com</link>
	<description>Various Data Mining Techniques</description>
	<lastBuildDate>Wed, 19 Jun 2013 21:10:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>Logical Functions &#8211; IIF()</title>
		<link>http://craftydba.com/?p=5756</link>
		<comments>http://craftydba.com/?p=5756#comments</comments>
		<pubDate>Wed, 19 Jun 2013 20:48:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tidbits]]></category>

		<guid isPermaLink="false">http://craftydba.com/?p=5756</guid>
		<description><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/decision-tree-30sec-rule.jpg"></a></p> <p>I am going continuing my short articles or tidbits on Transaction SQL <a href="http://msdn.microsoft.com/en-us/library/hh213226.aspx">logical functions</a>. </p> <p>These functions were introduced with the SQL Server 2012 release. Both functions have their origins from Visual Basic for Applications (<a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications">VBA</a>) framework. </p> <p>I think of these function as decision trees. Especially when they are nested [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/decision-tree-30sec-rule.jpg"><img src="http://craftydba.com/wp-content/uploads/2013/06/decision-tree-30sec-rule-300x300.jpg" alt="" title="decision-tree-30sec-rule" width="300" height="300" class="alignleft size-medium wp-image-5742" /></a></p>
<p>I am going continuing my short articles or tidbits on Transaction SQL <a href="http://msdn.microsoft.com/en-us/library/hh213226.aspx">logical functions</a>.  </p>
<p>These functions were introduced with the SQL Server 2012 release.  Both functions have their origins from Visual Basic for Applications (<a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications">VBA</a>) framework. </p>
<p>I think of these function as decision trees.  Especially when they are nested to several levels.  </p>
<p>If you have a kid like I do, you might find the decision tree for dropped food funny.  Please see image to left.</p>
<p>I will be exploring the <a href="http://msdn.microsoft.com/en-us/library/hh213574.aspx">IIF</a> function today. </p>
<p>The example below uses the <a href="http://msdn.microsoft.com/en-us/library/ms177610.aspx">RAND</a> function, to set two variables with random numbers between 1 and 10.  The immediate if function takes an expression to evaluate and two values.  It returns the first value if the expression is TRUE; otherwise, it returns the second value.</p>
<pre><span style="color: #008000;">--
--  Immediate If (IIF) Function
--

DECLARE @a int = CAST((RAND() * 10) AS INT) + 1;
DECLARE @b int = CAST((RAND() * 10) AS INT) + 1;
SELECT @a as var_a, @b as var_b, IIF ( @a > @b, 'TRUE', 'FALSE' ) AS a_gt_b;
GO 5
</span></pre>
</p>
<p>The output of each of the five batches is listed below.</p>
<pre><span style="color: #dd0000;">output: 

Beginning execution loop

var_a       var_b       a_gt_b
----------- ----------- ------
9           2           TRUE

var_a       var_b       a_gt_b
----------- ----------- ------
7           2           TRUE

var_a       var_b       a_gt_b
----------- ----------- ------
6           1           TRUE

var_a       var_b       a_gt_b
----------- ----------- ------
3           1           TRUE

var_a       var_b       a_gt_b
----------- ----------- ------
5           6           FALSE

Batch execution completed 5 times.

</span></pre>
</p>
<p>Please note the number five after the key word <a href="http://msdn.microsoft.com/en-us/library/ms188037.aspx">GO</a>.  This tells the transaction processor to run the commands (batch) five times.</p>
<p>In a nutshell, this function can be replaced by the <a href="http://msdn.microsoft.com/en-us/library/ms182717.aspx">IF &#8230; ELSE</a> control flow language.  This syntax has been part of the T-SQL language for a long time.</p>
]]></content:encoded>
			<wfw:commentRss>http://craftydba.com/?feed=rss2&#038;p=5756</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Logical Functions &#8211; CHOOSE()</title>
		<link>http://craftydba.com/?p=5729</link>
		<comments>http://craftydba.com/?p=5729#comments</comments>
		<pubDate>Sat, 15 Jun 2013 00:00:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tidbits]]></category>
		<category><![CDATA[CHOOSE]]></category>
		<category><![CDATA[database developer]]></category>
		<category><![CDATA[GO]]></category>
		<category><![CDATA[John F. Miner III]]></category>
		<category><![CDATA[Logic Functions]]></category>
		<category><![CDATA[RAND]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://craftydba.com/?p=5729</guid>
		<description><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/decision-tree-30sec-rule.jpg"></a></p> <p>I am going write a couple of tidbits on Transaction SQL <a href="http://msdn.microsoft.com/en-us/library/hh213226.aspx">logical functions</a>. </p> <p>These functions were introduced with the SQL Server 2012 release. Both functions have their origins from Visual Basic for Applications (<a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications">VBA</a>) framework. </p> <p>I think of these function as decision trees. Especially when they are nested to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/decision-tree-30sec-rule.jpg"><img src="http://craftydba.com/wp-content/uploads/2013/06/decision-tree-30sec-rule-300x300.jpg" alt="" title="decision-tree-30sec-rule" width="300" height="300" class="alignleft size-medium wp-image-5742" /></a></p>
<p>I am going write a couple of tidbits on Transaction SQL <a href="http://msdn.microsoft.com/en-us/library/hh213226.aspx">logical functions</a>.  </p>
<p>These functions were introduced with the SQL Server 2012 release.  Both functions have their origins from Visual Basic for Applications (<a href="http://en.wikipedia.org/wiki/Visual_Basic_for_Applications">VBA</a>) framework. </p>
<p>I think of these function as decision trees.  Especially when they are nested to several levels.  </p>
<p>If you have a kid like I do, you might find the decision tree for dropped food funny.  Please see image to left.</p>
<p>I will be exploring the <a href="http://msdn.microsoft.com/en-us/library/hh213019.aspx">CHOOSE</a> function today. </p>
<p>If you grew up like I did, you probably saw black and white re-runs of the <a href="http://en.wikipedia.org/wiki/The_Three_Stooges">Three Stooges</a> on television.</p>
<p>The example below uses the <a href="http://msdn.microsoft.com/en-us/library/ms177610.aspx">RAND</a> function, to get a random index value.  The index value is used with the CHOOSE() function to return the culprit who broke our chair.  </p>
<p>Many chairs were broken in the Three Stooges slapstick skits.</p>
<pre><span style="color: #008000;">--
--  Choose Function
--

-- Who broke the chair?
declare @idx int = CAST((RAND() * 3) AS INT) + 1;
select @idx as idx, CHOOSE(@idx, 'Moe', 'Larry', 'Curley') as Culprit
go 5

</span></pre>
</p>
<p>The output of each of the five batches is listed below.</p>
<pre><span style="color: #dd0000;">output: 

Beginning execution loop

idx         Culprit
----------- -------
1           Moe

idx         Culprit
----------- -------
2           Larry

idx         Culprit
----------- -------
3           Curley

idx         Culprit
----------- -------
2           Larry

idx         Culprit
----------- -------
3           Curley


Batch execution completed 5 times.
</span></pre>
</p>
<p>The function takes an index value N and a list of items.  It returns the Nth item in the list.</p>
<p>Please note the number five after the key word <a href="http://msdn.microsoft.com/en-us/library/ms188037.aspx">GO</a>.  This tells the transaction processor to run the commands (batch) five times.</p>
<p>Next time, I will be talking about the immediate if, <a href="http://msdn.microsoft.com/en-us/library/hh213574.aspx">IIF</a> function.</p>
]]></content:encoded>
			<wfw:commentRss>http://craftydba.com/?feed=rss2&#038;p=5729</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pragmatic Works &#8211; Performance Tuning</title>
		<link>http://craftydba.com/?p=5703</link>
		<comments>http://craftydba.com/?p=5703#comments</comments>
		<pubDate>Thu, 13 Jun 2013 00:00:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[Bradley Ball]]></category>
		<category><![CDATA[Kim Hathaway]]></category>
		<category><![CDATA[Microsoft New England Research & Development Center]]></category>
		<category><![CDATA[Nick Slevoski]]></category>
		<category><![CDATA[Performance Tuning]]></category>
		<category><![CDATA[Pragmatic Works]]></category>
		<category><![CDATA[SQL Training]]></category>

		<guid isPermaLink="false">http://craftydba.com/?p=5703</guid>
		<description><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/PragmaticHeaderLogo.png"></a></p> <p>I had the opportunity last week to attend the SQL Server Performance Tuning Workshop by <a href="http://pragmaticworks.com/LearningCenter/Workshops.aspx">Pragmatic </a>Works Learning Center. </p> <p>I definitely recommend this workshop.</p> <p>This is the second class I have taken at the <a href="http://microsoftcambridge.com/Default.aspx">Microsoft</a> New England Research and Development Center located in Cambridge, MA.</p> <p>It is conveniently located right [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/PragmaticHeaderLogo.png"><img class="alignleft size-full wp-image-5706" title="PragmaticHeaderLogo" src="http://craftydba.com/wp-content/uploads/2013/06/PragmaticHeaderLogo.png" alt="" width="166" height="29" /></a></p>
<p>I had the opportunity last week to attend the SQL Server Performance Tuning Workshop by <a href="http://pragmaticworks.com/LearningCenter/Workshops.aspx">Pragmatic </a>Works Learning Center.  </p>
<p><span style="color: #dd0000">I definitely recommend this workshop.</span></p>
<p>This is the second class I have taken at the <a href="http://microsoftcambridge.com/Default.aspx">Microsoft</a> New England Research and Development Center located in Cambridge, MA.</p>
<p>It is conveniently located right off the Kendall station stop on the Massachusetts Transit Bay Authority (MBTA) <a href="http://www.mbta.com/schedules_and_maps/subway/lines/?route=RED">Red Line</a>.</p>
<p>The main instructor for the class was Bradley Ball aka <a href="http://www.sqlballs.com/">SQLBalls</a>. He was a very likable speaker possessing good presentation skills as well as a wealth of knowledge to draw upon.</p>
<p>The following topics were covered during the two day workshop.</p>
<ol>
<li><span style="line-height: 1.6em;">Introduction: &#8216;Houston, do you have a problem?&#8217;</span></li>
<li><span style="line-height: 1.6em;">Hardware Environment and Configuration</span></li>
<li><span style="line-height: 1.6em;">Software Environment and Configuration</span></li>
<li><span style="line-height: 1.6em;">Understanding Queries in SQL Server</span></li>
<li><span style="line-height: 1.6em;">Index Tuning</span></li>
<li><span style="line-height: 1.6em;">Locking / Blocking / Deadlocks / Latches</span></li>
<li><span style="line-height: 1.6em;">Waits and Queues Methodology</span></li>
<li><span style="line-height: 1.6em;">Baselines</span></li>
<li><span style="line-height: 1.6em;">Setting Up Alerts</span></li>
<li><span style="line-height: 1.6em;">Extended Events and other Monitoring Tools</span></li>
<li><span style="line-height: 1.6em;">Making it real</span></li>
</ol>
<p>Other Pragmatic employees at the event were <a href="http://www.linkedin.com/pub/kim-hathaway/5/790/b5b">Kim Hathaway</a> (training) and <a href="http://www.linkedin.com/pub/nicholas-slevoski/41/802/24a">Nick Slevoski</a> (sales).</p>
<p>Kim presented several topics when Bradley needed a break and Nick made sure the event went on without a hitch.  </p>
<p>The food and refreshments were plenty and good.  <a href="http://mikehillwig.com/">Mike Hillwig</a>, a Microsoft SQL MVP, also attended the class.  </p>
<p>In short, if you have the time and money, please take the workshop.  You might even learn something like this old IT guy did.</p>
]]></content:encoded>
			<wfw:commentRss>http://craftydba.com/?feed=rss2&#038;p=5703</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SNESSUG &#8211; User Group</title>
		<link>http://craftydba.com/?p=5517</link>
		<comments>http://craftydba.com/?p=5517#comments</comments>
		<pubDate>Tue, 28 May 2013 17:57:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Pass Events]]></category>
		<category><![CDATA[Effective Data Warehouse Storage Patterns]]></category>
		<category><![CDATA[John F. Miner III]]></category>
		<category><![CDATA[PASS]]></category>
		<category><![CDATA[Presentation]]></category>
		<category><![CDATA[Rhode Island]]></category>
		<category><![CDATA[SNESSUG]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[User Group]]></category>

		<guid isPermaLink="false">http://craftydba.com/?p=5517</guid>
		<description><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/05/SNESSUG-logo.png"></a></p> <p>I am thrilled about presenting to the Southern New England SQL Server User Group (SNESSUG) on June 12, 2013. </p> <p>I always love the home team advantage by knowing the audience very well. It makes for a relaxing environment to teach and attendees to learn.</p> <p>Here is the gritty details of the presentation [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/05/SNESSUG-logo.png"><img src="http://craftydba.com/wp-content/uploads/2013/05/SNESSUG-logo-150x150.png" alt="" title="SNESSUG-logo" width="150" height="150" class="alignleft size-thumbnail wp-image-5523" /></a></p>
<p>I am thrilled about presenting to the Southern New England SQL Server User Group (SNESSUG) on June 12, 2013.  </p>
<p>I always love the home team advantage by knowing the audience very well.  It makes for a relaxing environment to teach and attendees to learn.</p>
<p>Here is the gritty details of the presentation that I will be exploring during the 90 minute talk.</p>
<p><span style="color: #008000;">Topic:</span></p>
<p>Effective Data Warehouse Storage Patterns</p>
<p><span style="color: #008000;">Abstract:</span></p>
<p>Many companies start off with a simple data mart for reporting.  As the company grows, users become dependent on the data mart for monitoring and making decisions on Key Performance Indicators (KPI).</p>
<p>Unexpected information growth in your data mart may lead to a performance impacted reporting system.  In short, your users will be lining up at your cube for their daily reports.</p>
<p><span style="color: #FF0000;">How do you reduce the size of your data mart and speed up data retrieval?</span></p>
<p>This presentation will review the following techniques to fix your woes.</p>
<p><span style="color: #008000;">Coverage:</span></p>
<p>1 &#8211; What is horizontal partitioning?<br />
2 &#8211; Database sharding for daily information.<br />
3 &#8211; Working with files and file groups.<br />
3 &#8211; Partitioned views for performance.<br />
4 &#8211; Table and Index partitions.<br />
5 &#8211; Row Data Compression.<br />
6 &#8211; Page Data Compression.<br />
7 &#8211; Programming a sliding window.<br />
8 &#8211; What are Federations in Azure SQL?</p>
<p><a href='http://craftydba.com/wp-content/uploads/2013/03/sql-pass-miner-effective-data-warehouse-storage-patterns.zip'>presentation bundle</a></p>
]]></content:encoded>
			<wfw:commentRss>http://craftydba.com/?feed=rss2&#038;p=5517</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Operator Precedence</title>
		<link>http://craftydba.com/?p=5692</link>
		<comments>http://craftydba.com/?p=5692#comments</comments>
		<pubDate>Sat, 11 May 2013 00:00:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tidbits]]></category>
		<category><![CDATA[database developer]]></category>
		<category><![CDATA[John F. Miner III]]></category>
		<category><![CDATA[Operator Precedence]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://craftydba.com/?p=5692</guid>
		<description><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/pyramid-operators-by-precedence.jpg"></a></p> <p>I am going to bring closure to my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. </p> <p>I will exploring the <a href="http://msdn.microsoft.com/en-us/library/ms190276.aspx">operator precedence</a> today. In mathematics and computer programming, the <a href="http://en.wikipedia.org/wiki/Order_of_operations">order of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/pyramid-operators-by-precedence.jpg"><img src="http://craftydba.com/wp-content/uploads/2013/06/pyramid-operators-by-precedence.jpg" alt="" title="pyramid-operators-by-precedence" width="134" height="120" class="alignleft size-full wp-image-5699" /></a></p>
<p>I am going to bring closure to my series of very short articles or tidbits on Transaction SQL Operators.  An operator is a symbol specifying an action that is performed on one or more expressions. </p>
<p>I will exploring the <a href="http://msdn.microsoft.com/en-us/library/ms190276.aspx">operator precedence</a> today.  In mathematics and computer programming, the <a href="http://en.wikipedia.org/wiki/Order_of_operations">order of operations</a> is a set of rules used to clarify which procedures should be performed first in a given mathematical expression.</p>
<p>I do not have enough time in this very short article to go over the eight levels that group and order expression evaluation.  However, I want to go over one example in which parentheses can be used to change the order of evaluation.</p>
<p>The first expression evaluates multiplication first since it is considered a higher level than subtraction.  The second example changes the order by using parentheses.</p>
<pre><span style="color: #008000;font-size:small;">--
-- Operator Precedence
--

-- Results in 2
select x = 2*3-2*2;

-- Results in 4
select 2*(3-2)*2 as y;

</span></pre>
</p>
<pre><span style="color: #dd0000;font-size:small;">-- ~ OUTPUT ~
x
-----------
2

y
-----------
4

</span></pre>
</p>
<p>If your Transaction SQL code is not evaluating the programming expression as you would like, consider using parentheses.  Next time I will be talking about <a href="http://msdn.microsoft.com/en-us/library/hh213226.aspx">logic functions</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://craftydba.com/?feed=rss2&#038;p=5692</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unary Operators</title>
		<link>http://craftydba.com/?p=5679</link>
		<comments>http://craftydba.com/?p=5679#comments</comments>
		<pubDate>Fri, 10 May 2013 00:00:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tidbits]]></category>
		<category><![CDATA[database developer]]></category>
		<category><![CDATA[John F. Miner III]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[Unary Operator]]></category>

		<guid isPermaLink="false">http://craftydba.com/?p=5679</guid>
		<description><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/roman-numeral-one.jpg"></a><br /> I am going to stick to writing my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. </p> <p>I will exploring the <a href="http://msdn.microsoft.com/en-us/library/ms188400.aspx">Unary Operators</a> today. In mathematics, a <a href="http://en.wikipedia.org/wiki/Unary_operation">unary</a> operation is an [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/roman-numeral-one.jpg"><img src="http://craftydba.com/wp-content/uploads/2013/06/roman-numeral-one.jpg" alt="" title="roman-numeral-one" width="110" height="106" class="alignleft size-full wp-image-5689" /></a><br />
I am going to stick to writing my series of very short articles or tidbits on Transaction SQL Operators.  An operator is a symbol specifying an action that is performed on one or more expressions. </p>
<p>I will exploring the <a href="http://msdn.microsoft.com/en-us/library/ms188400.aspx">Unary Operators</a> today.  In mathematics, a <a href="http://en.wikipedia.org/wiki/Unary_operation">unary</a> operation is an operation with only one operand.</p>
<p>Microsoft Transaction SQL has three unary operators:  <a href="http://msdn.microsoft.com/en-us/library/ms174362.aspx">positive</a>, <a href="http://msdn.microsoft.com/en-us/library/ms189480.aspx">negative</a>, and <a href="http://msdn.microsoft.com/en-us/library/ms173468.aspx">bitwise not</a>.  The example script below explores all three operators.</p>
<pre><span style="color: #008000;font-size:small;">--
--  Unary operators 
--

-- negative
declare @x int = 5;
select -@x as negative;

-- positive
declare @y int = 5;
select +@y as positive;

-- ones complement
declare @z int = 0x7F;
select ~ @z;

</span></pre>
</p>
<pre><span style="color: #dd0000;font-size:small;">-- ~ OUTPUT ~
negative
-----------
-5

positive
-----------
5

ones_complement
---------------
-128

</span></pre>
</p>
<p>I find the positive unary operator kinda-of useless.  It will not take a negative number and make it positive.  By default, a non-negative number is positive.  I think is part of the language for mathematical completeness.</p>
<p>To complete my exploration of operators, I will be talking about <a href="http://msdn.microsoft.com/en-us/library/ms190276.aspx">operator precedence</a> next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://craftydba.com/?feed=rss2&#038;p=5679</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String Operators</title>
		<link>http://craftydba.com/?p=5651</link>
		<comments>http://craftydba.com/?p=5651#comments</comments>
		<pubDate>Thu, 09 May 2013 00:00:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tidbits]]></category>
		<category><![CDATA[concatenate]]></category>
		<category><![CDATA[database developer]]></category>
		<category><![CDATA[John F. Miner III]]></category>
		<category><![CDATA[LIKE]]></category>
		<category><![CDATA[PATINDEX]]></category>
		<category><![CDATA[pattern matching]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[String Operator]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://craftydba.com/?p=5651</guid>
		<description><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/04/turquoise-yarn-md.png"></a><br /> I am going to press on with my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. </p> <p>I will exploring the <a href="http://msdn.microsoft.com/en-us/library/ms190301.aspx">String Operators</a> today. These operators can be broken down into two [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/04/turquoise-yarn-md.png"><img src="http://craftydba.com/wp-content/uploads/2013/04/turquoise-yarn-md-150x150.png" alt="" title="turquoise-yarn-md" width="150" height="150" class="alignleft size-thumbnail wp-image-5158" /></a><br />
I am going to press on with my series of very short articles or tidbits on Transaction SQL Operators.  An operator is a symbol specifying an action that is performed on one or more expressions. </p>
<p>I will exploring the <a href="http://msdn.microsoft.com/en-us/library/ms190301.aspx">String Operators</a> today.  These operators can be broken down into two categories: concatenation and pattern matching.  </p>
<p>Transaction SQL has both a <a href="http://msdn.microsoft.com/en-us/library/ms177561.aspx">simple</a> and <a href="http://msdn.microsoft.com/en-us/library/dd206992.aspx">compound</a> version of the concatenation operator.  This operator takes two strings and combines them into one.  The two examples below return the same output.  </p>
<pre><span style="color: #008000;font-size:x-small;">--
--  String operators - Concatenation
--

-- Declare variables
declare @front varchar(16) = 'Red ';
declare @back varchar(16) = 'Riding Hood ';
declare @phrase varchar(32) = '';

-- ~ simple concatenation ~
select @phrase = @front + @back;
select @phrase as grimm_tale

-- ~ compound concatenation ~
set @phrase = '';
set @phrase += @front;
set @phrase += @back;
select @phrase as grimm_tale
</span></pre>
</p>
<pre><span style="color: #dd0000;font-size:xx-small;">-- ~ OUTPUT ~
grimm_tale
--------------------------------
Red Riding Hood 

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms179884.aspx">[] operator</a> matches any single character within the specified range or set that is specified between the brackets.  The example below returns all employees with id&#8217;s like 22#.</p>
<pre><span style="color: #008000;font-size:x-small;">-- String Match - Character patterns
USE AdventureWorks2012;
GO 

-- Employees w/id of 22X - character match [] - 10 rows
SELECT 
    P.BusinessEntityID, P.FirstName, P.LastName, E.Gender, E.BirthDate
FROM 
    HumanResources.Employee AS E join Person.Person P
ON 
    E.BusinessEntityID = P.BusinessEntityID
where 
    E.BusinessEntityID like '[2][2][0-9]'
GO
</span></pre>
</p>
<pre><span style="color: #dd0000;font-size:xx-small;">-- ~ OUTPUT ~
BusinessEntityID FirstName                                          LastName                                           Gender BirthDate
---------------- -------------------------------------------------- -------------------------------------------------- ------ ----------
220              Karen                                              Berge                                              F      1970-01-25
221              Chris                                              Norred                                             M      1981-06-26
222              A. Scott                                           Wright                                             M      1962-10-19
223              Sairaj                                             Uddin                                              M      1982-01-22
224              William                                            Vong                                               M      1975-12-08
225              Alan                                               Brewer                                             M      1978-04-30
226              Brian                                              LaMee                                              M      1978-09-12
227              Gary                                               Altman                                             M      1965-03-21
228              Christian                                          Kleinerman                                         M      1970-02-18
229              Lori                                               Penor                                              F      1964-08-31

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms174424.aspx">_ operator</a> matches any single character in a string comparison operation that involves pattern matching such as <a href="http://msdn.microsoft.com/en-us/library/ms179859.aspx">LIKE</a> and <a href="http://msdn.microsoft.com/en-us/library/ms188395.aspx">PATINDEX</a>.  We have the same 10 records as output.</p>
<pre><span style="color: #008000;font-size:x-small;">-- String Match - Character patterns
USE AdventureWorks2012;
GO 

-- Employees w/id of 22X - character match _ - 10 rows
SELECT 
    P.BusinessEntityID, P.FirstName, P.LastName, E.Gender, E.BirthDate
FROM 
    HumanResources.Employee AS E join Person.Person P
ON 
    E.BusinessEntityID = P.BusinessEntityID
where 
    E.BusinessEntityID like '22_'
GO
</span></pre>
</p>
<pre><span style="color: #dd0000;font-size:xx-small;">-- ~ OUTPUT ~
BusinessEntityID FirstName                                          LastName                                           Gender BirthDate
---------------- -------------------------------------------------- -------------------------------------------------- ------ ----------
220              Karen                                              Berge                                              F      1970-01-25
221              Chris                                              Norred                                             M      1981-06-26
222              A. Scott                                           Wright                                             M      1962-10-19
223              Sairaj                                             Uddin                                              M      1982-01-22
224              William                                            Vong                                               M      1975-12-08
225              Alan                                               Brewer                                             M      1978-04-30
226              Brian                                              LaMee                                              M      1978-09-12
227              Gary                                               Altman                                             M      1965-03-21
228              Christian                                          Kleinerman                                         M      1970-02-18
229              Lori                                               Penor                                              F      1964-08-31

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms189454.aspx">% operator</a> matches any string of zero or more characters.  The output of this example adds one more record to the result set.  </p>
<pre><span style="color: #008000;font-size:x-small;">-- String Match - Character patterns
USE AdventureWorks2012;
GO 

-- Employees w/id of 22X - character match % - 11 rows
SELECT 
    P.BusinessEntityID, P.FirstName, P.LastName, E.Gender, E.BirthDate
FROM 
    HumanResources.Employee AS E join Person.Person P
ON 
    E.BusinessEntityID = P.BusinessEntityID
where 
    E.BusinessEntityID like '22%'
GO
</span></pre>
</p>
<pre><span style="color: #dd0000;font-size:xx-small;">-- ~ OUTPUT ~
BusinessEntityID FirstName                                          LastName                                           Gender BirthDate
---------------- -------------------------------------------------- -------------------------------------------------- ------ ----------
22               Sariya                                             Harnpadoungsataya                                  M      1981-06-21
220              Karen                                              Berge                                              F      1970-01-25
221              Chris                                              Norred                                             M      1981-06-26
222              A. Scott                                           Wright                                             M      1962-10-19
223              Sairaj                                             Uddin                                              M      1982-01-22
224              William                                            Vong                                               M      1975-12-08
225              Alan                                               Brewer                                             M      1978-04-30
226              Brian                                              LaMee                                              M      1978-09-12
227              Gary                                               Altman                                             M      1965-03-21
228              Christian                                          Kleinerman                                         M      1970-02-18
229              Lori                                               Penor                                              F      1964-08-31

</span></pre>
</p>
<p>Last but not least, the <a href="http://msdn.microsoft.com/en-us/library/ms188342.aspx">^ operator</a> returns any strings that do not match this character.  In the example below, we want to return all employees that have a first name that does not start with J.  The result set has 256 matches.  Shown below are the first five records.</p>
<pre><span style="color: #008000;font-size:x-small;">-- String Match - Character patterns
USE AdventureWorks2012;
GO 

-- Employees with first name does not start with J - 256 row
SELECT 
    P.BusinessEntityID, P.FirstName, P.LastName, E.Gender, E.BirthDate
FROM 
    HumanResources.Employee AS E join Person.Person P
ON 
    E.BusinessEntityID = P.BusinessEntityID
where 
    p.FirstName like '[^J]%'
GO
</span></pre>
</p>
<pre><span style="color: #dd0000;font-size:xx-small;">-- ~ OUTPUT ~
BusinessEntityID FirstName                                          LastName                                           Gender BirthDate
---------------- -------------------------------------------------- -------------------------------------------------- ------ ----------
1                Ken                                                Sánchez                                            M      1963-03-02
2                Terri                                              Duffy                                              F      1965-09-01
3                Roberto                                            Tamburello                                         M      1968-12-13
4                Rob                                                Walters                                            M      1969-01-23
5                Gail                                               Erickson                                           F      1946-10-29

</span></pre>
</p>
<p>In summary, concatenation operator + was defined as part of the TSQL syntax long before the <a href="http://msdn.microsoft.com/en-us/library/hh231515.aspx">CONCAT</a> function was ever introduced.  You are supplied with 4 pattern matching functions to slice and dice your data.  I am sure you will come across string operators in your scripting endeavors.  </p>
<p>Later today, I will be researching <a href="http://msdn.microsoft.com/en-us/library/ms188400.aspx">Unary Operators</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://craftydba.com/?feed=rss2&#038;p=5651</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Set Operators</title>
		<link>http://craftydba.com/?p=5617</link>
		<comments>http://craftydba.com/?p=5617#comments</comments>
		<pubDate>Wed, 08 May 2013 00:00:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tidbits]]></category>
		<category><![CDATA[ALL]]></category>
		<category><![CDATA[database developer]]></category>
		<category><![CDATA[EXCEPT]]></category>
		<category><![CDATA[INTERSECT]]></category>
		<category><![CDATA[John F. Miner III]]></category>
		<category><![CDATA[Set Operator]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[UNION]]></category>

		<guid isPermaLink="false">http://craftydba.com/?p=5617</guid>
		<description><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/set-operations-illustrated-with-venn-diagrams1.png"></a></p> <p>I am going to promote my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. </p> <p>I will exploring the <a href="http://msdn.microsoft.com/en-us/library/ff848745.aspx">Set Operators</a> today. A <a href="http://en.wikipedia.org/wiki/Venn_diagram">Venn diagram</a> is used to express different operations between [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/set-operations-illustrated-with-venn-diagrams1.png"><img src="http://craftydba.com/wp-content/uploads/2013/06/set-operations-illustrated-with-venn-diagrams1.png" alt="" title="set-operations-illustrated-with-venn-diagrams" width="499" height="482" class="aligncenter size-full wp-image-5620" /></a></p>
<p>I am going to promote my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. </p>
<p>I will exploring the <a href="http://msdn.microsoft.com/en-us/library/ff848745.aspx">Set Operators</a> today.  A <a href="http://en.wikipedia.org/wiki/Venn_diagram">Venn diagram</a> is used to express different operations between sets.  The above image represents the four possible operations given two sets, A and B, and three operators.  We will get into the operators supported by Microsoft Transaction SQL shortly. </p>
<p>To demonstrate these operators, we need a two temporary tables to query against.  The TSQL code below will create temporary tables A and B.</p>
<pre><span style="color: #008000;">--
-- ~ Setup Play Table ~
--

-- Drop existing table [A]
IF EXISTS 
(
    SELECT * 
    FROM tempdb.sys.objects T 
    WHERE T.TYPE = 'U' AND T.name LIKE '#A%'
) 
DROP TABLE #A;
GO

-- Make temp table [A]
CREATE TABLE #A (NUM INT PRIMARY KEY);
GO

-- Add data to table [A]
INSERT INTO #A VALUES (1), (2), (3), (4), (5), (6); 
GO

-- Drop existing table [B]
IF EXISTS 
(
    SELECT * 
    FROM tempdb.sys.objects T 
    WHERE T.TYPE = 'U' AND T.name LIKE '#B%'
) 
DROP TABLE #B;
GO

-- Make temp table [B]
CREATE TABLE #B (NUM INT PRIMARY KEY);
GO

-- Add data to table [B]
INSERT INTO #B VALUES (5), (6), (7), (8), (9), (10); 
GO

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms180026.aspx">UNION</a> operator combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union.  By default, the operator removes any duplicates.  Using set notation, the <span style="color: #dd0000;">A &#8746; B</span> notation is the union of both data sets in the above diagram.</p>
<p>To use this operator, the number and order of the columns have to be the same as well as the data types of the columns must be compatible.</p>
<pre><span style="color: #008000;">-- ~ UNION ~ no duplicates
SELECT * FROM #A
UNION
SELECT * FROM #B
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
NUM
-----------
1
2
3
4
5
6
7
8
9
10

</span></pre>
</p>
<p>Using the <a href="http://msdn.microsoft.com/en-us/library/ms180026.aspx">ALL</a> clause of the UNION operator shows the all rows including the duplicates.  Please note that the order of the rows in the result set is not guaranteed.  In our example, both values 5 and 6 are duplicated in the output list.</p>
<pre><span style="color: #008000;">-- ~ UNION ALL ~ show duplicates
SELECT * FROM #A
UNION ALL
SELECT * FROM #B
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
NUM
-----------
1
2
3
4
5
6
5
6
7
8
9
10

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms188055.aspx">EXCEPT</a> operator returns any distinct values from the left query that are not also found on the right query.  Using set notation, <span style="color: #dd0000;">A &#8211; B</span> is the EXCEPT operation of set A in regards to B.  Again, the number and order of the columns have to be the same as well as the data types of the columns must be compatible.</p>
<pre><span style="color: #008000;">-- ~ EXCEPT ~ ones that show just in A
SELECT * FROM #A
EXCEPT 
SELECT * FROM #B
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
NUM
-----------
1
2
3
4

</span></pre>
</p>
<p>Since this operator is order specific, the set notion <span style="color: #dd0000;">B &#8211; A</span> is the EXCEPT operation of set B in regards to A.  Please see the image above for a visual representation using set theory.</p>
<pre><span style="color: #008000;">-- ~ EXCEPT ~ ones that show just in B
SELECT * FROM #B
EXCEPT 
SELECT * FROM #A
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
NUM
-----------
7
8
9
10

</span></pre>
</p>
<p>Last but not least, the <a href="http://msdn.microsoft.com/en-us/library/ms188055.aspx">INTERSECT </a>operator returns any distinct values in common from both the left and right queries.  </p>
<p>Using set notation, <span style="color: #dd0000;">A &#8745; B</span> is the INTERSECT operation between sets A and B.  As usual, the number and order of the columns have to be the same as well as the data types of the columns must be compatible.</p>
<pre><span style="color: #008000;">-- ~ INTERSECT ~ ones that show in both A &#038; B
SELECT * FROM #A
INTERSECT
SELECT * FROM #B
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
NUM
-----------
5
6

</span></pre>
</p>
<p>To wrap-up this article, set operators are a great way to combine two similar query outputs using the UNION, EXCEPTION or INTERSECTION as combinatorial rules.  Next time, I will be researching the <a href="http://msdn.microsoft.com/en-us/library/ms190301.aspx">String Operators</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://craftydba.com/?feed=rss2&#038;p=5617</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Logical Operators</title>
		<link>http://craftydba.com/?p=5587</link>
		<comments>http://craftydba.com/?p=5587#comments</comments>
		<pubDate>Tue, 07 May 2013 00:00:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tidbits]]></category>
		<category><![CDATA[ALL]]></category>
		<category><![CDATA[AND]]></category>
		<category><![CDATA[ANY]]></category>
		<category><![CDATA[BETWEEN]]></category>
		<category><![CDATA[database developer]]></category>
		<category><![CDATA[EXISTS]]></category>
		<category><![CDATA[IN]]></category>
		<category><![CDATA[John F. Miner III]]></category>
		<category><![CDATA[LIKE]]></category>
		<category><![CDATA[Logical Operator]]></category>
		<category><![CDATA[NOT]]></category>
		<category><![CDATA[OR]]></category>
		<category><![CDATA[SOME]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://craftydba.com/?p=5587</guid>
		<description><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/17358309-true-and-false-check-box-written-on-a-blackboard.jpg"></a></p> <p>I am going to make headway on the series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. </p> <p>I will exploring the <a href=" http://msdn.microsoft.com/en-us/library/ms189773.aspx">Logical Operators</a> today. In short, these operators return either a boolean [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/06/17358309-true-and-false-check-box-written-on-a-blackboard.jpg"><img src="http://craftydba.com/wp-content/uploads/2013/06/17358309-true-and-false-check-box-written-on-a-blackboard.jpg" alt="" title="17358309-true-and-false-check-box-written-on-a-blackboard" width="168" height="109" class="alignleft size-full wp-image-5588" /></a></p>
<p>I am going to make headway on the series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. </p>
<p>I will exploring the <a href=" http://msdn.microsoft.com/en-us/library/ms189773.aspx">Logical Operators</a> today.  In short, these operators return either a boolean TRUE or FALSE.</p>
<p>To demonstrate many of these operators, we need a temporary table to query against.  The Transaction SQL code below creates a table that contains the first eight prime numbers.</p>
<pre><span style="color: #008000;">--
-- ~ Setup Play Table ~
--

-- Drop existing table
IF EXISTS 
(
    SELECT * 
    FROM tempdb.sys.objects T 
    WHERE T.TYPE = 'U' AND T.name LIKE '#PRIMES%'
) 
DROP TABLE #PRIMES;
GO

-- Make temp table
CREATE TABLE #PRIMES (NUM INT PRIMARY KEY);
GO

-- Add data to table
INSERT INTO #PRIMES VALUES 
    (2), (3), (5), (7),
    (11), (13), (17), (19);

-- Show the data
SELECT * FROM #PRIMES;
</span></pre>
</p>
<p>The output of the SELECT statement showing the prime numbers.</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
NUM
-----------
2
3
5
7
11
13
17
19

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms175064.aspx">SOME</a> operator compares a scalar value with a single column set of values.  It returns TRUE when one or more conditions are satisfied.</p>
<pre><span style="color: #008000;">-- ~ SOME OPERATOR ~
PRINT '13 LESS THAN [SOME] OF THE FIRST 8 PRIMES?' 
IF 13 < SOME (SELECT NUM FROM #PRIMES)
    PRINT 'TRUE' 
ELSE
    PRINT 'FALSE' ;
PRINT '';
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
13 LESS THAN [SOME] OF THE FIRST 8 PRIMES?
TRUE

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms175064.aspx">ANY</a> operator is equivalent to the SOME operator.  It returns FALSE when all of the conditions are not satisfied.</p>
<pre><span style="color: #008000;">-- ~ ANY OPERATOR ~
PRINT '23 LESS THAN [ANY] OF THE FIRST 8 PRIMES?' 
IF 23 < ANY (SELECT NUM FROM #PRIMES)
    PRINT 'TRUE' 
ELSE
    PRINT 'FALSE' ;
PRINT '';
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
23 LESS THAN [ANY] OF THE FIRST 8 PRIMES?
FALSE

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms178543.aspx">ALL</a> operator compares a scalar value with a single column set of values.  It returns TRUE when all of the conditions are satisfied.</p>
<pre><span style="color: #008000;">-- ~ ALL OPERATOR ~
PRINT '3 LESS THAN [ALL] OF THE FIRST 8 PRIMES?' 
IF 3 < ALL (SELECT NUM FROM #PRIMES)
    PRINT 'TRUE' 
ELSE
    PRINT 'FALSE' ;
PRINT '';
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
3 LESS THAN [ALL] OF THE FIRST 8 PRIMES?
FALSE

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms187922.aspx">BETWEEN</a> operator compares a scalar against a range of values.  It returns TRUE when the scalar is greater than or equal to the lower limit and less than or equal to the upper limit.</p>
<pre><span style="color: #008000;">-- ~ BETWEEN OPERATOR ~
PRINT 'AVERAGE OF FIRST 8 PRIMES IS [BETWEEN] 8 AND 10' 
IF (SELECT AVG(NUM) FROM #PRIMES) BETWEEN 8 AND 10 
    PRINT 'TRUE' 
ELSE
    PRINT 'FALSE' ;
PRINT '';
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
AVERAGE OF FIRST 8 PRIMES IS [BETWEEN] 8 AND 10
TRUE

</span></pre>
</p>
<p>The logical <a href="http://msdn.microsoft.com/en-us/library/ms188372.aspx">AND</a> operator combines two BOOLEAN expressions.  It returns TRUE if both conditions are true;  otherwise, it returns false.  The example below uses simple integer expressions to demonstrate this operator.</p>
<pre><span style="color: #008000;">-- ~ AND OPERATOR ~
PRINT 'TWO GREATER THAN ONE [AND] FOUR GREATER THAN THREE' 
IF (2 > 1) AND (4 > 3)
    PRINT 'TRUE' 
ELSE
    PRINT 'FALSE' ;
PRINT '';
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
TWO GREATER THAN ONE [AND] FOUR GREATER THAN THREE
TRUE

</span></pre>
</p>
<p>The logical <a href="http://msdn.microsoft.com/en-us/library/ms188361.aspx">OR</a> operator combines two BOOLEAN expressions.  It returns TRUE if any conditions are true;  otherwise, it returns false.  The example below uses simple integer expressions to demonstrate this operator.</p>
<pre><span style="color: #008000;">-- ~ OR OPERATOR ~
PRINT 'TWO GREATER THAN ONE [OR] FOUR LESS THAN THREE' 
IF (2 > 1) OR (4 < 3)
    PRINT 'TRUE' 
ELSE
    PRINT 'FALSE' ;
PRINT '';
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
TWO GREATER THAN ONE [OR] FOUR LESS THAN THREE
TRUE

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms179859.aspx">LIKE</a> operator determines if a character string matches a specific pattern.  The example below queries the sys.databases table and returns the row for the master database.</p>
<pre><span style="color: #008000;">-- ~ LIKE OPERATOR ~
SELECT name as dbms_name, database_id as dbms_id 
FROM master.sys.databases
WHERE name LIKE 'master%';
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
dbms_name  dbms_id
---------- -----------
master     1

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms177682.aspx">IN</a> operator ascertains if a value matches any elements in the sub-query or list of elements.  The example below queries the sys.databases table and returns a row for each system database.</p>
<pre><span style="color: #008000;">-- ~ IN OPERATOR ~
SELECT name as dbms_name, database_id as dbms_id 
FROM master.sys.databases
WHERE name IN ('tempdb', 'model', 'master', 'msdb')
ORDER BY database_id;
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
dbms_name  dbms_id
---------- -----------
master     1
tempdb     2
model      3
msdb       4

</span></pre>
</p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/ms189455.aspx">NOT</a> operator negates the BOOLEAN expression.  The example below queries the sys.databases table and returns the first 10 databases by id.  It removes the master database by using the NOT LIKE clause.</p>
<pre><span style="color: #008000;">-- ~ NOT OPERATOR ~
SELECT TOP 10 name as dbms_name, database_id as dbms_id 
FROM master.sys.databases
WHERE name NOT LIKE 'master%' 
ORDER BY database_id;
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
dbms_name            dbms_id
-------------------- -----------
tempdb               2
model                3
msdb                 4
ReportServer         5
ReportServerTempDB   6
MATH                 7
AdventureWorksDW2012 8
AUTOS                9
AdventureWorks2012   10
BASIC                11

</span></pre>
</p>
<p>Last but not least, the <a href="http://msdn.microsoft.com/en-us/library/ms188336.aspx">EXISTS</a> operator finds out if the sub-query contains at least one row.  The following example checks to see if the <a href="http://msdn.microsoft.com/en-us/library/aa992075.aspx">Adventure Works</a> database for 2012 is installed.</p>
<pre><span style="color: #008000;">-- ~ EXISTS OPERATOR ~
IF EXISTS (SELECT 1 FROM master.sys.databases WHERE name = 'AdventureWorks2012')
    PRINT 'TRUE - ADVENTURE WORKS IS INSTALLED' 
ELSE
    PRINT 'FALSE - ADVENTURE WORKS IS INSTALLED' ;
PRINT '';
</span></pre>
</p>
<pre><span style="color: #dd0000;">-- ~ OUTPUT ~
TRUE - ADVENTURE WORKS IS INSTALLED

</span></pre>
</p>
<p>Next time, I will be researching the <a href="http://msdn.microsoft.com/en-us/library/ff848745.aspx">Set Operators</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://craftydba.com/?feed=rss2&#038;p=5587</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Compound Operators</title>
		<link>http://craftydba.com/?p=5573</link>
		<comments>http://craftydba.com/?p=5573#comments</comments>
		<pubDate>Mon, 06 May 2013 00:00:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Tidbits]]></category>
		<category><![CDATA[Compound Operator]]></category>
		<category><![CDATA[database developer]]></category>
		<category><![CDATA[John F. Miner III]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://craftydba.com/?p=5573</guid>
		<description><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/05/math-symbols.jpg"></a></p> <p>I am going to forge ahead with my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. </p> <p>I will exploring the <a href="http://msdn.microsoft.com/en-us/library/cc645922.aspx">Compound Operators</a> today. These operators are a short hand for taking a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://craftydba.com/wp-content/uploads/2013/05/math-symbols.jpg"><img src="http://craftydba.com/wp-content/uploads/2013/05/math-symbols.jpg" alt="" title="math-symbols" width="175" height="147" class="alignleft size-full wp-image-5430" /></a></p>
<p>I am going to forge ahead with my series of very short articles or tidbits on Transaction SQL Operators. An operator is a symbol specifying an action that is performed on one or more expressions. </p>
<p>I will exploring the <a href="http://msdn.microsoft.com/en-us/library/cc645922.aspx">Compound Operators</a> today.  These operators are a short hand for taking a variable @V, applying some operator O and storing the result as variable @V.  A long way to write out adding 2 to variable @V is the expression @V = @V + 2 while the short way is the expression @V += 2.</p>
<p>T-SQL supports the following five compound math operations:  <a href="http://en.wikipedia.org/wiki/Addition">ADDITION</a>, <a href="http://en.wikipedia.org/wiki/Subtraction">SUBTRACTION</a>, <a href="http://en.wikipedia.org/wiki/Multiplication">MULTIPLICATION</a>, <a href="http://en.wikipedia.org/wiki/Division">DIVISION</a>, and <a href="http://en.wikipedia.org/wiki/Modulo_operation">MODULUS</a>.  </p>
<p>The examples below performs a simple calculation using each operator.</p>
<pre><span style="color: #008000;">--
--  Compound operators - Math (+-*/%)
--

declare @x tinyint = 0;

-- Addition
select @x += 4;
select @x as comp_addition

-- Subtraction
select @x -= 1;
select @x as comp_subtraction

-- Multiplication
select @x *= 4;
select @x as comp_multiplication

-- Division
select @x /= 2;
select @x as comp_division

-- Modulo
select @x %= 5;
select @x as comp_modulo
</span></pre>
</p>
<p>The output of each calculation is listed below.</p>
<pre><span style="color: #ff9900;">output: 

comp_addition
-------------
4

comp_subtraction
----------------
3

comp_multiplication
-------------------
12

comp_division
-------------
6

comp_modulo
-----------
1
</span></pre>
</p>
<p>In addition to math, T-SQL supports the following three compound bitwise operations:  <a href="https://en.wikipedia.org/wiki/Bitwise_operation#AND">AND</a>, <a href="https://en.wikipedia.org/wiki/Bitwise_operation#OR">OR</a> and <a href="https://en.wikipedia.org/wiki/Bitwise_operation#XOR">XOR</a>.  </p>
<p>The examples below performs a simple calculation using each operator.</p>
<pre><span style="color: #008000;">--
--  Compound operators - Bitwise (+-*/%)
--

-- Operator &#038; (AND)
declare @a tinyint = 0xFF;
set @a &#038;= 0x02;
print 'low_nibble_bit_two = 0x' + format(@a, 'X2');
print ' '; 

-- Operator | (OR)
declare @o tinyint = 0x00;
set @o |= 0x40;
print 'high_nibble_bit_three = 0x' + format(@o, 'X2');
print ' '; 

-- Operator | (XOR)
declare @x tinyint = 0xAA;
set @x ^= 0x55;
print 'all bits are on = 0x' + format(@x, 'X2');
print ' '; 

</span></pre>
</p>
<p>The output of each calculation is listed below.</p>
<pre><span style="color: #ff9900;">output: 

low_nibble_bit_two = 0x02
 
high_nibble_bit_three = 0x40
 
all bits are on = 0xFF

</span></pre>
</p>
<p>Please note that I have used both the <a href="http://msdn.microsoft.com/en-us/library/ms187330.aspx">SELECT</a> and <a href="http://msdn.microsoft.com/en-us/library/ms189484.aspx">SET</a> statements to perform assignments using compound operators.  This means that a variable can be used to get a summation from a query.</p>
<p>For instance, let&#8217;s believe the chief financial officer of <a href="http://msdn.microsoft.com/en-us/library/aa992075.aspx">Adventure Works</a> wanted us to get the hourly run rate for the company.  We want to only consider active employees, people who do not have a termination date.  The solution below uses a common table expression to find active employees.  </p>
<p>The @total variable is used with the addition compound operator to calculate a total hourly cost.</p>
<pre><span style="color: #008000;">--
--  Calculate hourly run rate
--

use [AdventureWorks2012];
declare @total as money = 0;
;
with cte_Active_Employees
as
(
select distinct
 BusinessEntityID
from 
  [HumanResources].[EmployeeDepartmentHistory]
where 
  EndDate is null
)
select 
  @total += Rate 
from 
  [HumanResources].[EmployeePayHistory] as h 
inner join 
  cte_Active_Employees as a
on 
  h.BusinessEntityID = a.BusinessEntityID
print 'Daily run rate is ' + format(@total, 'C') + ' per hour.';
</span></pre>
</p>
<pre><span style="color: #ff9900;">-- 
-- output: 
--

Daily run rate is $5,611.78 per hour.
</span></pre>
</p>
<p>Next time, I will be exploring the <a href="http://msdn.microsoft.com/en-us/library/ms189773.aspx">Logical Operators</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://craftydba.com/?feed=rss2&#038;p=5573</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
