<?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>DevExpertise &#187; SQL Server</title>
	<atom:link href="http://www.devexpertise.com/tag/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devexpertise.com</link>
	<description>Practical tips and tricks for all things .NET, SharePoint, Silverlight, InfoPath, and general application development.</description>
	<lastBuildDate>Wed, 12 May 2010 14:32:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Implementing a LINQ version of SQL&#8217;s LIKE Operator</title>
		<link>http://www.devexpertise.com/2009/09/25/implementing-a-linq-version-of-sqls-like-operator/</link>
		<comments>http://www.devexpertise.com/2009/09/25/implementing-a-linq-version-of-sqls-like-operator/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 18:30:51 +0000</pubDate>
		<dc:creator>DevExpert</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Extension Methods]]></category>

		<guid isPermaLink="false">http://www.devexpertise.com/2009/09/25/implementing-a-linq-version-of-sqls-like-operator/</guid>
		<description><![CDATA[One of the requirements of one of my recent projects was to implement a search page which allowed the user to enter a search term that supported wildcards.&#160; The search term could contain any number of wildcards in any position within that term.
If you’ve done anything like this before, you probably know there’s nothing built-in [...]]]></description>
			<content:encoded><![CDATA[<p>One of the requirements of one of my recent projects was to implement a search page which allowed the user to enter a search term that supported wildcards.&#160; The search term could contain any number of wildcards in any position within that term.</p>
<p>If you’ve done anything like this before, you probably know there’s nothing built-in to LINQ that supports this type of behavior.&#160; Sure, you could use a combination of <a href="http://msdn.microsoft.com/en-us/library/system.string.startswith.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">String.StartsWith</a>, <a href="http://msdn.microsoft.com/en-us/library/system.string.endswith.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">String.EndsWith</a>, or <a href="http://msdn.microsoft.com/en-us/library/system.string.contains.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">String.Contains</a>, but this could quickly become too cumbersome if there are many wildcards and/or they are scattered throughout the search term.&#160; Let’s look at a couple simple examples to illustrate…</p>
<p>Pretend for a second I was doing this in SQL, and I needed to get all values that start with the letter T.&#160; I would do this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">select </span><span style="color: gray">* </span><span style="color: blue">from </span>SomeTable <span style="color: blue">where </span>SomeField <span style="color: gray">LIKE </span><span style="color: red">'T%'</span></pre>
</div>
<p>
  <br />The .NET/LINQ equivalent would be this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">var </span>results = (<span style="color: blue">from </span>v <span style="color: blue">in </span>values <span style="color: blue">where </span>v.StartsWith(<span style="color: #a31515">&quot;T&quot;</span>) <span style="color: blue">select </span>v);</pre>
</div>
<p>
  <br />Not too difficult.&#160; However, what if you wanted to do the SQL-equivalent of this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">select </span><span style="color: gray">* </span><span style="color: blue">from </span>SomeTable <span style="color: blue">where </span>SomeField <span style="color: gray">LIKE </span><span style="color: red">'%a%a%'</span></pre>
</div>
<p>
  <br />You’d have do a little creative parsing.&#160; It gets even worse when you as the developer doesn’t know what search term will be entered, how many wildcards will be included, and where in the term they appear.&#160; It all has to be dynamic. </p>
<p>I did a little poking around to see if anyone has done this before, and the only thing I could find was recommendations on using StartsWith/EndsWith/Contains, which I already ruled out.&#160; I also found the <a href="http://msdn.microsoft.com/en-us/library/system.data.linq.sqlclient.sqlmethods.like.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">SqlMethods.Like()</a> method which sounded perfect.&#160; However after further research, discovered it can <em>only</em> be used on an entity directly retrieved from a DataContext, such as this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">using </span>(<span style="color: #2b91af">DemoDataContext </span>db = <span style="color: blue">new </span><span style="color: #2b91af">DemoDataContext</span>()){
    <span style="color: blue">var </span>results = (<span style="color: blue">from </span>v <span style="color: blue">in </span>db.SomeTable <span style="color: blue">where </span><span style="color: #2b91af">SqlMethods</span>.Like(v.SomeField, <span style="color: #a31515">&quot;*a*a*&quot;</span>) <span style="color: blue">select </span>v);
}</pre>
</div>
<p>
  <br />If you try to use the SqlMethods.Like() method on anything except a DataContext’s Table&lt;T&gt;, you’ll get the following message:</p>
<blockquote>
<p><strong><font color="#ff0000">“Method &#8216;Boolean Like(System.String, System.String)&#8217; cannot be used on the client; it is only for translation to SQL.”</font></strong></p>
</blockquote>
<p>So much for that.&#160; I decided to write my own extension method.&#160; I figured I could write one fairly easily using a regular expression, and I was right!&#160; I checked out a trusty <a href="http://regexlib.com/CheatSheet.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/regexlib.com');">RegEx cheat sheet</a> and found the following relevant metacharacters:</p>
<ul>
<li><font size="1" face="cour"><strong>^</strong>&#160;&#160;&#160;&#160; </font>Indicates the start of a string </li>
<li><font size="1" face="cour"><strong>$</strong>&#160;&#160;&#160;&#160; </font>Indicates the end of a string </li>
<li><font size="1" face="cour"><strong>* </strong>&#160;&#160;&#160; </font>Indicates zero or more of previous expression </li>
</ul>
<p>Knowing this, I wrote the following extension method:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">public static bool </span>Like(<span style="color: blue">this string </span>value, <span style="color: blue">string </span>term) {
    <span style="color: #2b91af">Regex </span>regex = <span style="color: blue">new </span><span style="color: #2b91af">Regex</span>(<span style="color: blue">string</span>.Format(<span style="color: #a31515">&quot;^{0}$&quot;</span>, term.Replace(<span style="color: #a31515">&quot;*&quot;</span>, <span style="color: #a31515">&quot;.*&quot;</span>)), <span style="color: #2b91af">RegexOptions</span>.IgnoreCase);
    <span style="color: blue">return </span>regex.IsMatch(value ?? <span style="color: blue">string</span>.Empty);
}</pre>
</div>
<p>
  <br />Which I can then use like this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">var </span>results = (<span style="color: blue">from </span>v <span style="color: blue">in </span>values <span style="color: blue">where </span>v.Like(<span style="color: #a31515">&quot;*a*a*&quot;</span>) <span style="color: blue">select </span>v);</pre>
</div>
<p>
  <br />I can even simplify this by wrapping it up in another extension method:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">string</span>&gt; Like(<span style="color: blue">this </span><span style="color: #2b91af">IEnumerable</span>&lt;<span style="color: blue">string</span>&gt; source, <span style="color: blue">string </span>expression) {
    <span style="color: blue">return </span>(<span style="color: blue">from </span>s <span style="color: blue">in </span>source <span style="color: blue">where </span>s.Like(expression) <span style="color: blue">select </span>s);
}</pre>
</div>
<p>
  <br />Now all I have to do is the following:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">var </span>results = values.Like(<span style="color: #a31515">&quot;*a*a*&quot;</span>);</pre>
</div>
<p>
  <br />Finally, a quick usage example to prove it works:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 2200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">var </span>values = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt;(){
    <span style="color: #a31515">&quot;Widget&quot;</span>, <span style="color: #a31515">&quot;Gadget&quot;</span>, <span style="color: #a31515">&quot;Whatchamacallit&quot;</span>, <span style="color: #a31515">&quot;Gizmo&quot;</span>,
    <span style="color: #a31515">&quot;Thingamabob&quot;</span>, <span style="color: #a31515">&quot;Thingamajig&quot;</span>, <span style="color: #a31515">&quot;Doodad&quot;</span>, <span style="color: #a31515">&quot;Doohickey&quot;</span>};

<span style="color: blue">var </span>results = values.Like(<span style="color: #a31515">&quot;*a*a*&quot;</span>);

<span style="color: blue">foreach </span>(<span style="color: blue">string </span>result <span style="color: blue">in </span>results) {
    <span style="color: #2b91af">Console</span>.WriteLine(result);
}</pre>
</div>
<p>
  <br />Which outputs:</p>
<p><a href="http://www.devexpertise.com/wp-content/uploads/ImplementingaLINQversionofSQLsLIKEOperat_C261/image.png"  rel="lightbox"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.devexpertise.com/wp-content/uploads/ImplementingaLINQversionofSQLsLIKEOperat_C261/image_thumb.png" width="669" height="86" /></a> </p>
<p>&#160;</p>
<p>Hopefully you’ll find this useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devexpertise.com/2009/09/25/implementing-a-linq-version-of-sqls-like-operator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Tip/Trick: Retrieving Records as a Comma-Delimited Value Using FOR XML and STUFF</title>
		<link>http://www.devexpertise.com/2009/04/14/sql-tiptrick-retrieving-records-as-a-comma-delimited-value-using-for-xml-and-stuff/</link>
		<comments>http://www.devexpertise.com/2009/04/14/sql-tiptrick-retrieving-records-as-a-comma-delimited-value-using-for-xml-and-stuff/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 12:50:49 +0000</pubDate>
		<dc:creator>DevExpert</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.devexpertise.com/2009/04/14/sql-tiptrick-retrieving-records-as-a-comma-delimited-value-using-for-xml-and-stuff/</guid>
		<description><![CDATA[Once in awhile I run across a need to create a delimited list from a set of rows in a SQL table.&#160; The old-school method is to use a cursor to iterate over these rows and build a dynamic string.&#160; As we all know, cursors are expensive and should be avoided whenever possible.&#160; I recently [...]]]></description>
			<content:encoded><![CDATA[<p>Once in awhile I run across a need to create a delimited list from a set of rows in a SQL table.&#160; The old-school method is to use a cursor to iterate over these rows and build a dynamic string.&#160; As we all know, cursors are expensive and should be avoided whenever possible.&#160; I recently found a better way to accomplish this and wanted to share it in case you’ve run into this before.</p>
<p>For this example, I’m going to be working with a simple Cities table that contains a City field and a State field.&#160; A simple SELECT statement yields the following result:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">select </span><span style="color: gray">* </span><span style="color: blue">from </span>Cities <span style="color: blue">where State </span><span style="color: gray">= </span><span style="color: red">'New York'</span></pre>
</div>
<p><a href="http://www.devexpertise.com/wp-content/uploads/2009/04/image5.png"  rel="lightbox"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.devexpertise.com/wp-content/uploads/2009/04/image-thumb5.png" width="186" height="139" /></a> </p>
</p>
<p>
  <br />The first step is to transform this result into a chunk of XML, and for that the <a href="http://msdn.microsoft.com/en-us/library/ms345137.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">FOR XML</a> statement is perfect.&#160; Consider the following SQL:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">declare </span>@xml <span style="color: blue">varchar</span><span style="color: gray">(</span>1000<span style="color: gray">)
</span><span style="color: blue">set </span>@xml <span style="color: gray">= (</span><span style="color: blue">select </span>City <span style="color: blue">from </span>Cities <span style="color: blue">where State </span><span style="color: gray">= </span><span style="color: red">'New York' </span><span style="color: blue">for xml path</span><span style="color: gray">(</span><span style="color: red">''</span><span style="color: gray">))</span></pre>
</div>
<p>
  <br />This returns the following (I trimmed it down a little to fit on the page, but it does return all rows):</p>
<blockquote>
<p><strong>&lt;City&gt;New York City&lt;/City&gt;&lt;City&gt;Buffalo&lt;/City&gt;&lt;City&gt;Rochester&lt;/City&gt;</strong></p>
</blockquote>
<p>Now that we have an XML string, we can just use the built-in REPLACE functions to remove the XML nodes:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">set </span>@xml <span style="color: gray">= </span><span style="color: magenta">replace</span><span style="color: gray">(</span>@xml<span style="color: gray">, </span><span style="color: red">'&lt;City&gt;'</span><span style="color: gray">, </span><span style="color: red">','</span><span style="color: gray">)
</span><span style="color: blue">set </span>@xml <span style="color: gray">= </span><span style="color: magenta">replace</span><span style="color: gray">(</span>@xml<span style="color: gray">, </span><span style="color: red">'&lt;/City&gt;'</span><span style="color: gray">,</span><span style="color: red">''</span><span style="color: gray">)</span></pre>
</div>
<p>
  <br />This returns the following:</p>
<blockquote>
<p><strong>,New York City,Buffalo,Rochester,Yonkers,Syracuse</strong></p>
</blockquote>
<p>Now, the only thing left is to remove the beginning space and comma from this for which the new SQL 2008 <a href="http://msdn.microsoft.com/en-us/library/ms188043.aspx" onclick="javascript:pageTracker._trackPageview('/outbound/article/msdn.microsoft.com');">STUFF</a> function is perfect:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">select </span><span style="color: magenta">stuff</span><span style="color: gray">(</span>@xml<span style="color: gray">,</span>1<span style="color: gray">,</span>1<span style="color: gray">,</span><span style="color: red">''</span><span style="color: gray">)</span></pre>
</div>
<p>
  <br />This returns the following, which is exactly what we need:</p>
<blockquote>
<p><strong>New York City,Buffalo,Rochester,Yonkers,Syracuse</strong></p>
</blockquote>
<p>Now, chances are you don’t want to have to declare variables and do all this in multiple operations.&#160; This can all be wrapped into a single select statement, which makes it easy to implement:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre class="code"><span style="color: blue">select </span><span style="color: magenta">stuff</span><span style="color: gray">((</span><span style="color: magenta">replace</span><span style="color: gray">(</span><span style="color: magenta">replace</span><span style="color: gray">(
       (</span><span style="color: blue">select </span>City <span style="color: blue">from </span>Cities <span style="color: blue">where State </span><span style="color: gray">= </span><span style="color: red">'New York' </span><span style="color: blue">for xml path</span><span style="color: gray">(</span><span style="color: red">''</span><span style="color: gray">)), </span><span style="color: red">'&lt;City&gt;'</span><span style="color: gray">, </span><span style="color: red">','
       </span><span style="color: gray">), </span><span style="color: red">'&lt;/City&gt;'</span><span style="color: gray">,</span><span style="color: red">''</span><span style="color: gray">)),</span>1<span style="color: gray">,</span>1<span style="color: gray">,</span><span style="color: red">''</span><span style="color: gray">)</span></pre>
</div>
<p>&#160;</p>
<p>That’s it!&#160; Pretty slick, huh?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devexpertise.com/2009/04/14/sql-tiptrick-retrieving-records-as-a-comma-delimited-value-using-for-xml-and-stuff/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
