<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Solution for the &amp;quot;LIKE vs. ?&amp;quot; Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/10/01/solution-for-the-like-vs-puzzle.aspx</link><description>In late April, I posted a puzzle to test readers' knowledge of SQL Server query processing internals . The goal of the puzzle was to take a simple yet incredibly inefficient query and rewrite it, without changing the base tables or adding any additional</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Solution for the "LIKE vs. ?" Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/10/01/solution-for-the-like-vs-puzzle.aspx#9173</link><pubDate>Wed, 01 Oct 2008 21:18:27 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9173</guid><dc:creator>Jason Massie</dc:creator><description>&lt;p&gt;Gordon is a tsql madman. We tried but failed to get him to name is son Harry Klundt.&lt;/p&gt;
</description></item><item><title>re: Solution for the "LIKE vs. ?" Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/10/01/solution-for-the-like-vs-puzzle.aspx#9174</link><pubDate>Wed, 01 Oct 2008 21:33:42 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9174</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;I should probably delete your post to keep a sense of decorum about this blog, but ... that was a much-needed bit of humor after a tremendously boring day, so it stays. &amp;nbsp;Thanks for the comment!&lt;/p&gt;
</description></item><item><title>re: Solution for the "LIKE vs. ?" Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/10/01/solution-for-the-like-vs-puzzle.aspx#9177</link><pubDate>Wed, 01 Oct 2008 23:52:02 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9177</guid><dc:creator>Jonathan Kehayias</dc:creator><description>&lt;p&gt;Can you recheck this:&lt;/p&gt;
&lt;p&gt;SELECT *&lt;/p&gt;
&lt;p&gt;FROM b1&lt;/p&gt;
&lt;p&gt;JOIN b2 ON&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;'#'+b1.blat1+'#' = '#'+LEFT(b2.blat2,7)+'#'&lt;/p&gt;
&lt;p&gt;That doesn't pass the test when I run it. &amp;nbsp;Is there a typo in it perhaps? &amp;nbsp;I only get 2 rows back from running that.&lt;/p&gt;
</description></item><item><title>re: Solution for the "LIKE vs. ?" Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/10/01/solution-for-the-like-vs-puzzle.aspx#9180</link><pubDate>Thu, 02 Oct 2008 01:34:30 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9180</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Hi Jonathan,&lt;/p&gt;
&lt;p&gt;Sorry, I left it coded for 7 characters which was the example Kevan sent me. &amp;nbsp;Change the 7 to a 5 and it should work. &amp;nbsp;Or, rebuild b1 with blat1 typed as CHAR(7) and change the insert to do LEFT(AddressLine1, 7), and then leave the 7 and you'll see the same result as if you'd run b2.blat2 LIKE b1.blat1 + '%'. &amp;nbsp;Making the same change to the base table and using the LEFT(b2.blat2,7) = b1.blat1 predicate will in fact NOT return the same exact results.&lt;/p&gt;
&lt;p&gt;I hope that clarifies things?&lt;/p&gt;
</description></item><item><title>re: Solution for the "LIKE vs. ?" Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/10/01/solution-for-the-like-vs-puzzle.aspx#9186</link><pubDate>Thu, 02 Oct 2008 17:49:10 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9186</guid><dc:creator>Bruce M</dc:creator><description>&lt;p&gt;Thanks for the answers! Two questions:&lt;/p&gt;
&lt;p&gt;1. What is the joke? I didn't get it :(&lt;/p&gt;
&lt;p&gt;2. Can you please explain the trick of using delimiters &amp;quot;#&amp;quot; for the input strings?&lt;/p&gt;
</description></item><item><title>re: Solution for the "LIKE vs. ?" Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/10/01/solution-for-the-like-vs-puzzle.aspx#9188</link><pubDate>Thu, 02 Oct 2008 20:40:14 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9188</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Hi Bruce,&lt;/p&gt;
&lt;p&gt;The fact that you didn't get the joke is probably a good thing. &amp;nbsp;Hint: if you want to get it, just put your mind firmly in the gutter. &amp;nbsp;It's really quite sophomoric (but after my day yesterday, it was just what I needed).&lt;/p&gt;
&lt;p&gt;The delimiters work because the data is CHAR(n), and if the input data length is &amp;lt; N, it will end up with trailing spaces. &amp;nbsp;Those trailing spaces will end up being ignored with an equality predicate, but with LIKE, when you append the '%' they are preserved. &amp;nbsp;So you end up getting different results for the equality (more rows returned) vs the LIKE predicate. &amp;nbsp;Delimiting the data makes those rows not matched, because just as when adding the '%' those trailing spaces are preserved. &amp;nbsp;I guess, in retrospect, you don't need to delimit the front of the string too, but arguably it's nice for consistency.&lt;/p&gt;
</description></item><item><title>re: Solution for the "LIKE vs. ?" Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/10/01/solution-for-the-like-vs-puzzle.aspx#9255</link><pubDate>Sat, 04 Oct 2008 19:30:49 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9255</guid><dc:creator>Bruce M</dc:creator><description>&lt;p&gt;Oooh! Now I get it...initially, I thought it had something to do with t-sql.&lt;/p&gt;
&lt;p&gt;Thanks for the explanation of the delimiters. Isn't that the same as using LTRIM? Or is using a function in a join slow down performance?&lt;/p&gt;
</description></item><item><title>re: Solution for the "LIKE vs. ?" Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/10/01/solution-for-the-like-vs-puzzle.aspx#9996</link><pubDate>Wed, 19 Nov 2008 00:38:37 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9996</guid><dc:creator>Brian </dc:creator><description>&lt;p&gt;Hi Adam,&lt;/p&gt;
&lt;p&gt;I don't imagine such an elegant solution is possible for the Like VS ? puzzle if the original question would have been:&lt;/p&gt;
&lt;p&gt;SELECT *&lt;/p&gt;
&lt;p&gt;FROM b1JOIN b2 &lt;/p&gt;
&lt;p&gt;ON &amp;nbsp; &amp;nbsp;b2.blat2 &lt;/p&gt;
&lt;p&gt;LIKE '%' + b1.blat1 + '%'&lt;/p&gt;
&lt;p&gt;Nonetheless, I found this blog post very interesting, but you have an idea let us know.&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;
</description></item><item><title>re: Solution for the "LIKE vs. ?" Puzzle</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2008/10/01/solution-for-the-like-vs-puzzle.aspx#10079</link><pubDate>Mon, 24 Nov 2008 22:02:03 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:10079</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Hi Brian,&lt;/p&gt;
&lt;p&gt;Sure there is--it's called Full Text Search :-)&lt;/p&gt;
</description></item></channel></rss>