<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Squarespace Site Server v5.11.81 (http://www.squarespace.com/) on Sat, 11 Feb 2012 10:40:18 GMT--><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rss="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:cc="http://web.resource.org/cc/"><rss:channel rdf:about="http://www.pocketjoshua.com/sql-journal/"><rss:title>PocketJoshua - T-SQL</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/</rss:link><rss:description>Pocket Joshua's guides to better coding with T-QL and MS SQL Server</rss:description><dc:language>en-US</dc:language><dc:date>2012-02-11T10:40:18Z</dc:date><admin:generatorAgent rdf:resource="http://www.squarespace.com/">Squarespace Site Server v5.11.81 (http://www.squarespace.com/)</admin:generatorAgent><rss:items><rdf:Seq><rdf:li rdf:resource="http://www.pocketjoshua.com/sql-journal/learn-regex-in-t-sql-kick-as2ass.html"/><rdf:li rdf:resource="http://www.pocketjoshua.com/sql-journal/presenting-at-phillynet-code-camp-20112-oct-15.html"/><rdf:li rdf:resource="http://www.pocketjoshua.com/sql-journal/data-modeling-software-tools-list.html"/><rdf:li rdf:resource="http://www.pocketjoshua.com/sql-journal/data-truncation-handling-techniques.html"/><rdf:li rdf:resource="http://www.pocketjoshua.com/sql-journal/presenting-at-pssug-june-9-2010-meeting.html"/><rdf:li rdf:resource="http://www.pocketjoshua.com/sql-journal/the-dangers-of-failing-to-understand-t-sqls-distinct-and-gro.html"/><rdf:li rdf:resource="http://www.pocketjoshua.com/sql-journal/debunking-sql-server-myths.html"/><rdf:li rdf:resource="http://www.pocketjoshua.com/sql-journal/phillynet-code-camp-presentation-the-fine-art-of-stored-proc.html"/><rdf:li rdf:resource="http://www.pocketjoshua.com/sql-journal/presenting-at-phillynet-code-camp-20101.html"/><rdf:li rdf:resource="http://www.pocketjoshua.com/sql-journal/sql-saturday-37-ndash-philadelphia-presentation-notes.html"/></rdf:Seq></rss:items></rss:channel><rss:item rdf:about="http://www.pocketjoshua.com/sql-journal/learn-regex-in-t-sql-kick-as2ass.html"><rss:title>Learn RegEx in T-SQL &amp; kick as{2}|a[s][s]+</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/learn-regex-in-t-sql-kick-as2ass.html</rss:link><dc:creator>Joshua Lynn</dc:creator><dc:date>2011-10-17T13:18:05Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p><span class="full-image-float-right ssNonEditable"><span> <img src="http://www.pocketjoshua.com/storage/post-images/PDNCodeCamp.png?__SQUARESPACE_CACHEVERSION=1318880626667" alt="" /> </span></span></p>
<p>Thanks to all who attended the Learn RegEx in TSQL presentation at the Philly.Net Code Camp 2011.2. &nbsp;Especially those who took the time to catch up with me throughout the day.</p>
<p>RegEx is a big topic and is hard to learn in only an hour and 20 minutes.  I do hope that you got enough information to get yourself started in learning how to use RegEx in TSQL in your own environment.</p>
<p>All the links discussed in the presentation are included throughout this post.  The PowerPoint  was not included since it seemed kind of pointless without me also attached to the presentation. Some of the content was extracted and included in this post.</p>
<p><span class="full-image-inline ssNonEditable"><span><img src="http://www.pocketjoshua.com/storage/post-images/CodeCamp_2011-2-RegExSession.jpg?__SQUARESPACE_CACHEVERSION=1318883638910" alt="" /></span><span class="thumbnail-caption" style="width: 200px;">The Class!</span></span></p>
<p><span class="full-image-inline ssNonEditable"><span><span class="full-image-inline ssNonEditable"><span><img style="width: 200px;" src="http://www.pocketjoshua.com/storage/Code_Camp_Heros_2011-2.JPG?__SQUARESPACE_CACHEVERSION=1318883593661" alt="" /></span><span class="thumbnail-caption" style="width: 200px;">Code Camp Heroes Bill and Rob</span></span><br /></span></span></p>
<h2>Code:</h2>
<p><a class="offsite-link-inline" href="http://goo.gl/AchxI" target="_blank">Down Load The code</a> for the presentation.</p>
<p>The file 01 - installRegexAssembly.sql is my version of the create assembley and create of the RegEx functions in the [util] schema.</p>
<h2>Goal:</h2>
<h3>Get you started using RegEx in TSQL</h3>
<ul>
<li>Load the toolbox with techniques&nbsp;</li>
<li>Exposure to variations&nbsp;</li>
<li>Under the hood knowledge</li>
</ul>
<p>&nbsp;</p>
<h2>Warning</h2>
<ul>
<li>&nbsp;There are many ways to write a Regular Expression!</li>
<li>RegEx isn&rsquo;t inherently better then other solutions, even in TSQL.</li>
</ul>
<p>&nbsp;</p>
<h2>What Are Regular Expressions?</h2>
<h3>Tools for matching and manipulating text RegEx pattern = as{2}</h3>
<h4>Will Match</h4>
<ul>
<li>ass</li>
<li>assume</li>
<li>passenger</li>
<li>class</li>
</ul>
<h4>Will NOT match</h4>
<ul>
<li>fast</li>
<li>cake</li>
<li>salad</li>
</ul>
<p>&nbsp;</p>
<h2>Why use RegEx?</h2>
<h3>Over standard TSQL RegEx can:</h3>
<ul>
<li>Be faster</li>
<li>Be easier to code</li>
<li>Require fewer steps</li>
<li>Accomplish more complex tasks</li>
</ul>
<p>&nbsp;</p>
<h2>What can RegEx be used for?</h2>
<ul>
<li>Validate Data Format&nbsp;</li>
<li>Scrub Data&nbsp;</li>
<li>Same as standard TSQL text functions but allow for more complex criteria&nbsp;                           
<ul>
<li>Replace()&nbsp;</li>
<li>LIKE &lsquo;%&rsquo; &nbsp;--Technically not a function but you get the idea</li>
<li>PATINDEX()&nbsp;</li>
<li>SUBSTRING() </li>
</ul>
</li>
</ul>
<p>&nbsp;</p>
<h2>How to use RegEx in TSQL?</h2>
<p>&nbsp;</p>
<p><span class="full-image-block ssNonEditable"><span><img style="width: 400px;" src="http://www.pocketjoshua.com/storage/post-images/RegEx_In_SQL.png?__SQUARESPACE_CACHEVERSION=1318858808404" alt="" /></span><span class="thumbnail-caption" style="width: 400px;">Formula for RegEx in TSQL </span></span></p>
<p>SQL 2005 and up supports .Net CLR for User Defined Functions</p>
<p>.Net has built in base class library for RegEx: <a class="offsite-link-inline" href="http://goo.gl/23RbV" target="_blank">System.Text.RegularExpressions</a></p>
<p>Combine above two facts -&gt; RegEx in TSQL</p>
<p>&nbsp;</p>
<h2>Now how do we actually use RegEx in TSQL?</h2>
<p>Write the code for the .NET based UDFs ourselves&nbsp;</p>
<p>OR</p>
<p>Use someone else's code!</p>
<p><a class="offsite-link-inline" href="http://goo.gl/YkP9W" target="_blank">http://www.simple-talk.com/sql/t-sql-programming/clr-assembly-regex-functions-for-sql-server-by-example/</a></p>
<p>The is an excellent article by Phil Factor over at Simple-talk. &nbsp;There are a few parts to this article including the basics of writing .NET based functions in TSQL. &nbsp;There is also a file that lets you jump start using RegEx in TSQL.</p>
<p>Keep in mind you will need to be running SQL 2005 or newer for this code to work. &nbsp;It will also work on express versions of SQL server too. &nbsp;The CLR will also need to be enabled for the database you want to install the RegEx functions.</p>
<p>&nbsp;There are 4 files at the top of the article you can download:</p>
<ol>
<li>InstallRegex.sql</li>
<li>installRegexAssembly.sql</li>
<li>installRegexSample.sql</li>
<li>RegexSQLCLR.vb</li>
</ol>
<p>The fourth file is a text file with the VB code that gets compiled in to the DLL. &nbsp;You do not need to do this unless you are curious and interested in the details.</p>
<p>To jump right in to using the TSQL UDFs in your data base just run the first to files. &nbsp;The first one, despite its name creates the Assembly on the SQL db. &nbsp;The second creates the functions in TSQL that are implemented by the code in the assembly.</p>
<p>NOTE: The code linked to above installs the functions in the [dbo] schema. &nbsp;The version used in the presentation was modified to create a new Schema [util] and install the assembly and functions under that schema.</p>
<p>&nbsp;</p>
<h2>Overview of the RegEx Functions</h2>
<p>The functions installed by the SQL scripts will create 9 funtions that will be useable in your TSQL code. &nbsp;The break down as follows based on return type:</p>
<p>&nbsp;</p>
<ul>
<li>Boolean&nbsp;               
<ul>
<li>RegExIsMatch()&nbsp;</li>
</ul>
</li>
<li>Text Returning&nbsp;               
<ul>
<li>RegExReplace&nbsp;()&nbsp;</li>
<li>RegExReplaceX&nbsp;()</li>
<li>RegExMatch()</li>
</ul>
</li>
<li>Table Valued&nbsp;               
<ul>
<li>RegExSplit()&nbsp;</li>
<li>RegExMatches ()&nbsp;</li>
</ul>
</li>
<li>Integer&nbsp;               
<ul>
<li>RegExIndex()&nbsp;</li>
</ul>
</li>
<li>RegEx Utility&nbsp;               
<ul>
<li>RegExEscape()&nbsp;</li>
<li>RegExOptionEnumeration()</li>
</ul>
</li>
</ul>
<p>&nbsp;</p>
<h2>Tips for Learning RegEx patterns</h2>
<p>Have a good cheat sheet. Here are some I've used</p>
<p><a class="offsite-link-inline" href="http://goo.gl/ORTw5" target="_blank">http://www.regular-expressions.info/reference.html</a></p>
<p><a class="offsite-link-inline" href="http://goo.gl/stgKL" target="_blank">http://regexlib.com/CheatSheet.aspx</a></p>
<p><a class="offsite-link-inline" href="http://goo.gl/usLRA" target="_blank">http://regexlib.com/DisplayPatterns.aspx</a>&nbsp;This link is to a libray of expressions for matching common patterns.</p>]]></content:encoded></rss:item><rss:item rdf:about="http://www.pocketjoshua.com/sql-journal/presenting-at-phillynet-code-camp-20112-oct-15.html"><rss:title>Presenting at Philly.Net Code Camp 2011.2 Oct 15</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/presenting-at-phillynet-code-camp-20112-oct-15.html</rss:link><dc:creator>Joshua Lynn</dc:creator><dc:date>2011-10-06T20:05:04Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p><img style="margin: 0px 10px 0px 0px; display: inline" alt="" src="http://www.pocketjoshua.com/storage/post-images/PhillyDotNetLogo.png" /></p> 
<p>I will be presenting at Philly.Net Code Camp 2011.2 on Oct. 15 http://goo.gl/yYGXv</p>

<p>Presentation will be: &nbsp;Learn RegEx in T-SQL &amp; kick \</p>
<p>Session Description: (This is what users will see, please go for concise and elegant) Regular Expressions, aka RegEx, is a powerful syntax for finding patterns in text, which has been around since the 60’s. Until recently the power of RegEx has been an elusive tool to T-SQL Developers. This presentation will provide an introduction to Regular Expressions in general and concentrate on the tools and techniques for using them in T-SQL. Find out how easy and powerful Regular Expressions can be for everyday tasks such as basic where condition criteria to validating and scrubbing data. Come on you know you’ve always wanted to learn RegEx ever since you first saw them. Now is your chance to get a jumpstart on using them. You will leave with the tools, knowledge and resources to get you started.
</p>]]></content:encoded></rss:item><rss:item rdf:about="http://www.pocketjoshua.com/sql-journal/data-modeling-software-tools-list.html"><rss:title>Data Modeling Software Tools List</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/data-modeling-software-tools-list.html</rss:link><dc:creator>Joshua Lynn</dc:creator><dc:date>2010-10-11T12:45:48Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p>Embarcadero's ER/Studio has been my data modeling tool of choice for over a decade.&nbsp; There are many good things about the product but there are also things I don't like or wish were better or different.&nbsp;&nbsp; It is also an expensive software package, approximately $2K for a SQL server only version plus around $800 for annual maintainence including upgrades.</p>
<p>Roughly once a year I get a hankering to check out the grass on the the other side of the fence.&nbsp; This hankering hit me a few weeks ago.&nbsp; Google brought me to the following website that had a nice list of tools with a brief description and cost:</p>
<p><a href="http://www.databaseanswers.org/modelling_tools.htm">http://www.databaseanswers.org/modelling_tools.htm</a>﻿</p>
<p>A few of the tools caught my eye and I downloaded and tested a few.&nbsp; The short answer is I'm sticking with my trusted albeit not perfect ER/Studio.&nbsp; The good news is the quality and sophistication of the affordable tools has improved greatly.&nbsp; Here is a list of a few of the tools that made the short list:</p>
<p><a href="http://www.modelright.com/">ModelRight 3.6</a></p>
<p><a href="http://www.dbconstructor.com">dbconstructor</a></p>
<p><a href="http://www.thekompany.com/products/dataarchitect/">DataArchitect</a></p>
<p><a href="http://www.datanamic.com/dezign/index.html">DeZine for Databases</a></p>
<p>When I did my testing I didn't document my findings and impressions of these packages for the purpose of posting the information.&nbsp; If I needed a tool to get some work done without spending a lot of money there are definitely a few out there that would be useful, but there are no must have superstars in the pack.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>]]></content:encoded></rss:item><rss:item rdf:about="http://www.pocketjoshua.com/sql-journal/data-truncation-handling-techniques.html"><rss:title>Data Truncation Handling Techniques</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/data-truncation-handling-techniques.html</rss:link><dc:creator>Joshua Lynn</dc:creator><dc:date>2010-06-09T12:42:50Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p>Question:</p>  <blockquote>   <p>we are trying to get this to error because the value is getting truncated. What do i need to change on our warning settings?      <br />SELECT CONVERT(varchar(10), 'adededddasdfasdf')</p> </blockquote>  <p>&#160;</p>  <p>On a regular basis I use CONVERT(VARCHAR(10), GETDATE(), 101) to convert</p>  <p>2010-06-09 08:03:07.313&#160;&#160;&#160; to&#160;&#160;&#160; 2010-06-09</p>  <p>I'm counting on the truncation to cut out the data I don't want. I don't recall ever getting a truncation error in this case.&#160; So there may not be a setting to cause this to occur.&#160; Unfortunately, I do not know the direct answer to the question.</p>  <p>On the other hand truncation errors are common when doing ETL projects.&#160; Here is an example of what does cause a truncation error to bubble up.</p>  <pre class="brush: sql;">CREATE TABLE #MyTable ( MyField VARCHAR(10) NOT NULL ) 

INSERT INTO #MyTable ( MyField )VALUES ( 'abcdefghij') 
INSERT INTO #MyTable ( MyField )VALUES ( '12345678901112') 

SELECT * FROM #MyTable AS mt </pre>

<p>&#160;</p>

<p>Executing this code will have the following in the message window:</p>

<p>(1 row(s) affected) 
  <br /><span style="color: #ff0000">Msg 8152, Level 16, State 14, Line 10 
    <br />String or binary data would be truncated. 

    <br /></span>The statement has been terminated.</p>

<p>(1 row(s) affected)</p>

<p>This code will cause a truncation error to abort the second insert statement.&#160;&#160; The error is useful and important because it helps maintain data integrity.&#160; On the other hand it would be nice to find a way to avoid the problem in the first place</p>

<p>Here are some techniques for preemptively dealing with truncation errors:</p>

<p>When dealing with unstable or unverified data sources a good approach is to validate the data prior to loading. Typically this involves loading the data in to varchar(max) fields and running various metadata inspections such as LEN(), ISNUMERIC(), ISDATE(), etc.</p>

<p>Yes it takes time, but in an automated process it is more important to run without error and be able to generate meaningful error reports on what data is flawed.</p>

<pre class="brush: sql;">---------------------------------------
-- In line validation
---------------------------------------
;
WITH SourceData AS (    
    SELECT 'adededddasdfasdf' AS MyField
    UNION ALL SELECT 'abcdefghij'
    UNION ALL SELECT '12345678901112'
    UNION ALL SELECT '1234567890'
)
SELECT
      CONVERT(VARCHAR(10), MyField) AS MyField_Len10
    , CASE    
        WHEN LEN(MyField) &lt;= 10 THEN 0
        ELSE 1
      END AS IsTruncated
FROM 
    SourceData


---------------------------------------
-- List of truncation errors - with option raised error if it's really needed
---------------------------------------
DECLARE @ErrorCount    INT
SET @ErrorCount = 0
;
WITH SourceData AS (    
    SELECT 'adededddasdfasdf' AS MyField
    UNION ALL SELECT 'abcdefghij'
    UNION ALL SELECT '12345678901112'
    UNION ALL SELECT '1234567890'
)
SELECT
      CONVERT(VARCHAR(10), MyField) AS MyField_Len10
FROM 
    SourceData
WHERE
    LEN(MyField) &gt; 10    

SET @ErrorCount = @@ROWCOUNT
IF @ErrorCount != 0
    BEGIN
        RAISERROR('There were %d truncation errors', 16,1,@ErrorCount)
    END</pre>

<p>A more useful approach to check for the error case.</p>

<pre class="brush: sql;">GO
---------------------------------------
-- Alt solution without returning data
---------------------------------------
DECLARE @ErrorCount    INT
SET @ErrorCount = 0
;
WITH SourceData AS (    
    SELECT 'adededddasdfasdf' AS MyField
    UNION ALL SELECT 'abcdefghij'
    UNION ALL SELECT '12345678901112'
    UNION ALL SELECT '1234567890'
)
SELECT
     @ErrorCount = COUNT(*) 
FROM 
    SourceData
WHERE
    LEN(MyField) &gt; 10    

--SET @ErrorCount = @@ROWCOUNT
IF @ErrorCount != 0
    BEGIN
        RAISERROR('There were %d truncation errors', 16,1,@ErrorCount)
    END</pre>

<p>&#160;</p>

<p>The RAISERROR code is there to illustrate how to cause an error to bubble up if that is the desired behavior.</p>

<p><em>Note: The IF EXISTS() syntax would also have worked well in this case.&#160; However, <a href="sql-journal/2008/10/27/cannot-use-cte-with-exists-clause.html">If EXISTS()&#160; doesn’t work with CTEs</a> and the error count would not have been available for the error message.</em></p>]]></content:encoded></rss:item><rss:item rdf:about="http://www.pocketjoshua.com/sql-journal/presenting-at-pssug-june-9-2010-meeting.html"><rss:title>Presenting at PSSUG June 9, 2010 Meeting</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/presenting-at-pssug-june-9-2010-meeting.html</rss:link><dc:creator>Joshua Lynn</dc:creator><dc:date>2010-06-03T18:45:47Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p><a href="http://www.pocketjoshua.com/resource/WindowsLiveWriter-PresentingatPSSUGJune92010Meeting_CF9E-?fileId=7192183"><img style="border-bottom: 0px; border-left: 0px; margin: 0px 5px 5px 0px; display: inline; border-top: 0px; border-right: 0px" title="PSSUGlogo_sm" border="0" alt="PSSUGlogo_sm" align="left" src="http://www.pocketjoshua.com/resource/WindowsLiveWriter-PresentingatPSSUGJune92010Meeting_CF9E-?fileId=7192184" width="144" height="63" /></a> </p>  <p>I will be the keynote speaker at the <a title="PSSUG June 2010 meeting" href="http://www.pssug.org/Events/June92010/tabid/837/Default.aspx" target="_blank">June 9, 2010 monthly meeting of the Philadelphia SQL Server User Group</a> at the Malvern, PA&#160; office of Microsoft.&#160; The presentation will be “Part II” of the “Fine Art of Stored Procedures and User Defined Functions.” </p>  <p>Part I was given recently at the Philly.Net Code Camp 2010.1 back in April.&#160; That presentation went fine but I was disappointed that I didn’t have enough time to get to the really meaty code portion.&#160; </p>  <p>Part II leaves off where Part I ended with only a touch of repetition.&#160; If you caught the Code Camp Presentation this will be overwhelmingly new content.&#160; If you missed it don’t worry, the key ideas from Part I will be covered but without the detail.&#160; Instead the presentation will look at real issues and factors that are important to crafting beautiful sprocs and UDFs and show the technique with code.</p>  <p><a href="http://www.pocketjoshua.com/resource/WindowsLiveWriter-PresentingatPSSUGJune92010Meeting_CF9E-?fileId=7192186"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="PSSUG Combo Logo" border="0" alt="PSSUG Combo Logo" src="http://www.pocketjoshua.com/resource/WindowsLiveWriter-PresentingatPSSUGJune92010Meeting_CF9E-?fileId=7192187" width="244" height="95" /></a></p>]]></content:encoded></rss:item><rss:item rdf:about="http://www.pocketjoshua.com/sql-journal/the-dangers-of-failing-to-understand-t-sqls-distinct-and-gro.html"><rss:title>The Dangers of Failing to Understand T-SQL&amp;rsquo;s DISTINCT and GROUP BY</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/the-dangers-of-failing-to-understand-t-sqls-distinct-and-gro.html</rss:link><dc:creator>Joshua Lynn</dc:creator><dc:date>2010-05-17T13:59:18Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<h2>Example of the Difference Between DISTINCT and GROUP BY</h2>  <p>The distinct keyword in a T-SQL query to ensure an unique result set is often a quick and easy solution, which is often fraught with peril and performance issues.&#160; The key difference between GROUP BY and DISTINCT is when the aggregation or elimination of duplicates occurs during the query processing step.&#160; The distinct operation is completed after the non distinct result set has materialized.&#160; Thus it can not take advantage of indexes or early reduction of unnecessary data such as the GROUP BY clause can achieve.</p>  <p>The code snippet below is a simple example of when and how the aggregation or elimination of duplicate data occurs.&#160; The data isn’t all that important except to realize that it does contain some duplicates in the data of interest.</p>  <pre class="brush: sql;">DECLARE @MyData TABLE(
    SomeValue    VARCHAR(3) NOT NULL
)

INSERT INTO @MyData ( SomeValue ) VALUES ( 'AAA')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'BBB')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'CCC')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'AAA')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'BBB')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'AAA')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'AAA')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'BBB')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'CCC')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'AAA')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'BBB')
INSERT INTO @MyData ( SomeValue ) VALUES ( 'AAA')


SELECT DISTINCT
      ROW_NUMBER() OVER(ORDER BY md.SomeValue) AS rn
    , SomeValue
FROM 
    @MyData AS md    


SELECT 
      ROW_NUMBER() OVER(ORDER BY md.SomeValue) AS rn
    , SomeValue
FROM 
    @MyData AS md    
GROUP BY
      SomeValue    </pre>

<p>&#160;</p>

<table style="width: 255pt; border-collapse: collapse" border="0" cellspacing="0" cellpadding="0" width="340"><colgroup><col style="width: 39pt; mso-width-source: userset; mso-width-alt: 1901" width="52" /><col style="width: 72pt; mso-width-source: userset; mso-width-alt: 3510" width="96" /><col style="width: 48pt" width="64" /></colgroup><tbody>
    <tr style="height: 15pt" height="20">
      <td style="width: 111pt; height: 15pt" class="style1" height="20" width="148" colspan="2">Result Set # 1<span style="mso-spacerun: yes">&#160;</span></td>

      <td style="width: 48pt" width="64"></td>

      <td style="width: 96pt" class="style1" width="128" colspan="2">Result Set # 2<span style="mso-spacerun: yes">&#160;</span></td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style2" height="20">rn</td>

      <td class="style2">SomeValue</td>

      <td></td>

      <td class="style2">1</td>

      <td class="style2">AAA</td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">1</td>

      <td class="style3">AAA</td>

      <td></td>

      <td class="style3">2</td>

      <td class="style3">BBB</td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">2</td>

      <td class="style3">AAA</td>

      <td></td>

      <td class="style3">3</td>

      <td class="style3">CCC</td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">3</td>

      <td class="style3">AAA</td>

      <td></td>

      <td></td>

      <td></td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">4</td>

      <td class="style3">AAA</td>

      <td></td>

      <td></td>

      <td></td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">5</td>

      <td class="style3">AAA</td>

      <td></td>

      <td></td>

      <td></td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">6</td>

      <td class="style3">AAA</td>

      <td></td>

      <td></td>

      <td></td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">7</td>

      <td class="style3">BBB</td>

      <td></td>

      <td></td>

      <td></td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">8</td>

      <td class="style3">BBB</td>

      <td></td>

      <td></td>

      <td></td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">9</td>

      <td class="style3">BBB</td>

      <td></td>

      <td></td>

      <td></td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">10</td>

      <td class="style3">BBB</td>

      <td></td>

      <td></td>

      <td></td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">11</td>

      <td class="style3">CCC</td>

      <td></td>

      <td></td>

      <td></td>
    </tr>

    <tr style="height: 15pt" height="20">
      <td style="height: 15pt" class="style3" height="20">12</td>

      <td class="style3">CCC</td>

      <td></td>

      <td></td>

      <td></td>
    </tr>
  </tbody></table>

<p>&#160;</p>

<p>Looking at the results it should be evident that the first query using the DISTINCT directive didn't apply the de-duplication phase until after the Row Numbers were uniquely generated.&#160; Thus there were no records to eliminate due to duplication.</p>

<p>The second result set has the results that may have been expected in that only the unique non-duplicate set of data was returned with a simple row number.</p>

<p>The take-away from this is not that the GROUP BY clause is correct, rather that there are differences between the DISTINCT directive and the GROUP BY clause.&#160; They are not always interchangeable from a data perspective.&#160; The performance issue is a whole other discussion.</p>]]></content:encoded></rss:item><rss:item rdf:about="http://www.pocketjoshua.com/sql-journal/debunking-sql-server-myths.html"><rss:title>Debunking SQL Server Myths</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/debunking-sql-server-myths.html</rss:link><dc:creator>Joshua Lynn</dc:creator><dc:date>2010-05-04T19:51:29Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p>Paul Randal of SQLSkills has completed a nice series on debunking misconceptions of SQL server.</p>
<p><a class="offsite-link-inline" href="http://www.sqlskills.com/BLOGS/PAUL/category/Misconceptions.aspx" target="_blank">http://www.sqlskills.com/BLOGS/PAUL/category/Misconceptions.aspx</a></p>
<p>Some of the things I knew, some I was completely wrong about and others I just didn't know about.&nbsp; The bulk of the misconceptions I had stemmed from working with SQL Serve since the mid 90&rsquo;s going back to version 6.5.&nbsp; You do your best to learn and keep up with the new releases but can get caught in the trap of not knowing some detail you once knew has changed.</p>
<p>Focusing on just the new features is not enough.&nbsp; We must also take the time to learn the details and some internals of things that are changed.&nbsp; They can have real implications of who database should be designed and optimized.</p>]]></content:encoded></rss:item><rss:item rdf:about="http://www.pocketjoshua.com/sql-journal/phillynet-code-camp-presentation-the-fine-art-of-stored-proc.html"><rss:title>Philly.Net Code Camp presentation &amp;ndash; The Fine Art of Stored Procedures and User Defined Functions</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/phillynet-code-camp-presentation-the-fine-art-of-stored-proc.html</rss:link><dc:creator>Joshua Lynn</dc:creator><dc:date>2010-04-12T13:09:34Z</dc:date><dc:subject>Fine Art of SQL Presentaions</dc:subject><content:encoded><![CDATA[<p><img style="margin: 0px 10px 0px 0px; display: inline" alt="" src="http://www.pocketjoshua.com/storage/post-images/PhillyDotNetLogo.png" /></p>  <p><img style="margin: 0px 10px 0px 0px; display: inline" alt="" align="left" src="http://www.pocketjoshua.com/storage/post-images/CodeCamp2010.1._Small.jpg" /></p>  <p>The presentation of “<a href="http://codecamp.phillydotnet.org/2010-1/Lists/Sessions/DispForm.aspx?ID=146&amp;Source=http%3A%2F%2Fcodecamp.phillydotnet.org%2F2010-1%2FLists%2FSessions%2FBy%2520Track.aspx" target="_blank">The Fine Art of Stored Procedures and User Defined Functions</a>” at Philly.Net's Code Camp 2010.1 on Saturday April 10, 2010 during the 10AM Session went reasonably well.&#160; As this was the fist time giving this presentation there is much cleanup to do in focusing&#160; the talking points and the demos.&#160; There was more material not covered then we actually had time to go through.&#160; So it seems that this could easily be a two part series.</p>  <p>Kevin Goff’s presentation had an Idea in it that I have thought about before but never took the time to implement.&#160; His presentation had way more material then there was time for.&#160; So he built a menu into his Power Point Presentation and allowed the attendees to pick the ones they wanted to cover.&#160; I like this because it allows the presenter to develop all the material and have a flexible presentation based on the audience.&#160; The “Fine Art of SQL” series is a good candidate for this Style.</p>  <p>I will submit the second half or more of this presentation to Code Camp or PSSUG for upcoming events.&#160; The next one I will aim towards an intermediate level instead of the beginner level.&#160; This way we can spend more time on code and technique then theory and book knowledge.</p>  <p>Don’t forget to complete the <a href="http://http://codecamp.phillydotnet.org/2010-1/Lists/Evaluation%202/NewForm.aspx?Source=http%3A%2F%2Fcodecamp.phillydotnet.org%2F2010-1%2FLists%2FEvaluation%25202%2Foverview.aspx" target="_blank">online evaluations</a>.</p>  <p>Additionally, I will attempt to post various sections of this presentation as a continuing series on this blog.&#160; I a bout of shameless self-promotion please subscribe to the <a href="http://www.pocketjoshua.com/sql-journal/rss.xml" target="_blank">RSS feed</a>.</p>  <p>Here are some links that may be of use:</p>  <p>The presentation with code <a href="http://www.pocketjoshua.com/storage/code-samples/fine_art_of_sql/The%20Fine%20Art%20of%20Stored%20Procedures%20%20User%20Defined%20Functions.zip" target="_blank">here</a></p>  <h2>MS SQL Books Online:</h2>  <ul>   <li><a href="http://msdn.microsoft.com/en-us/library/ms187926.aspx" target="_blank">CREATE PROCEDURE</a> </li>    <li><a href="http://msdn.microsoft.com/en-us/library/ms186755.aspx" target="_blank">CREATE FUNCTION</a> </li>    <li><a href="http://msdn.microsoft.com/en-us/library/ms190782.aspx" target="_blank">Stored Procedures Explained</a> – Explore the links on this page </li>    <li><a href="http://msdn.microsoft.com/en-us/library/ms189593.aspx" target="_blank">UDF’s Explained</a> – Explore the links on this page </li>    <li><a href="http://msdn.microsoft.com/en-us/library/ms181055(v=SQL.100).aspx" target="_blank">Execution Plan Caching and Reuse</a> </li>    <li><a href="http://msdn.microsoft.com/en-us/library/ms175580(v=SQL.100).aspx" target="_blank">Parameters and Execution Plan Reuse</a> </li> </ul>  <h2>Going Deeper On Microsoft&#160; TechNet :</h2>  <ul>   <li><a href="http://technet.microsoft.com/en-us/library/cc966419.aspx#EHKAE" target="_blank">Statistics Used by the Query Optimizer in Microsoft SQL Server 2005</a> </li>    <li><a href="Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005 " target="_blank">Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005</a> </li> </ul>]]></content:encoded></rss:item><rss:item rdf:about="http://www.pocketjoshua.com/sql-journal/presenting-at-phillynet-code-camp-20101.html"><rss:title>Presenting at Philly.NET Code Camp 2010.1</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/presenting-at-phillynet-code-camp-20101.html</rss:link><dc:creator>Joshua Lynn</dc:creator><dc:date>2010-04-03T21:13:07Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p><img style="margin: 0px 10px 0px 0px; display: inline;" src="http://www.pocketjoshua.com/storage/post-images/PhillyDotNetLogo.png" alt="" /></p>
<p>I will be presenting &ldquo;<a href="http://codecamp.phillydotnet.org/2010-1/Lists/Sessions/DispForm.aspx?ID=146&amp;Source=http%3A%2F%2Fcodecamp.phillydotnet.org%2F2010-1%2FLists%2FSessions%2FBy%2520Track.aspx" target="_blank">The Fine Art of Stored Procedures and User Defined Functions</a>&rdquo; at Philly.Net's Code Camp 2010.1 on Saturday April 10, 2010 during the 10AM Session.</p>
<p>For Registration and Event Information: <a href="http://codecamp.phillydotnet.org/2010-1/default.aspx">http://codecamp.phillydotnet.org/2010-1/default.aspx</a></p>]]></content:encoded></rss:item><rss:item rdf:about="http://www.pocketjoshua.com/sql-journal/sql-saturday-37-ndash-philadelphia-presentation-notes.html"><rss:title>SQL Saturday #37 &amp;ndash; Philadelphia Presentation Notes</rss:title><rss:link>http://www.pocketjoshua.com/sql-journal/sql-saturday-37-ndash-philadelphia-presentation-notes.html</rss:link><dc:creator>Joshua Lynn</dc:creator><dc:date>2010-03-13T18:40:11Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p><a href="http://www.sqlsaturday.com/viewsession.aspx?sat=37&amp;sessionid=1396" target="_blank"><img style="margin: 0px 5px 5px 0px; display: inline" align="left" src="http://www.sqlsaturday.com/images/sql_saturday_logo_small.gif" /></a>Thanks to all the people who packed the classroom for the presentation, asked good questions, willingly stayed awake and were generally very kind in their appreciation.&#160; </p>  <p>Download the PPT and the Code</p>  <p><a href="http://www.pocketjoshua.com/storage/code-samples/t-sql-for-performance/T-SQL%20Coding%20with%20XML.zip">http://www.pocketjoshua.com/storage/code-samples/t-sql-for-performance/T-SQL%20Coding%20with%20XML.zip</a></p>  <p>Check out all the code samples and slides.&#160; There were more demos in the folder then we had time to cover.</p>  <p>Here are a few recommended links for improving your performance skills wit T-SQL:</p>  <p><a href="http://www.simple-talk.com/sql/performance/writing-efficient-sql-set-based-speed-phreakery/" target="_blank">Simple-talk.com Writing Efficient SQL: Set-Based Speed Phreakery</a>     <br /><a href="http://www.simple-talk.com/sql/performance/writing-efficient-sql-set-based-speed-phreakery/">http://www.simple-talk.com/sql/performance/writing-efficient-sql-set-based-speed-phreakery/</a></p>  <p>Aaron Bertrand blog series on bad habits    <br /><a title="http://www.pocketjoshua.com/sql-journal/bad-habits-to-kick-blog-series-worth-the-time.html" href="http://www.pocketjoshua.com/sql-journal/bad-habits-to-kick-blog-series-worth-the-time.html">http://www.pocketjoshua.com/sql-journal/bad-habits-to-kick-blog-series-worth-the-time.html</a></p>  <p>Of course there is also this blog.&#160; Feel free to add the rss feed to your favorite reader.   <br /><a title="http://www.pocketjoshua.com/sql-journal/rss.xml" href="http://www.pocketjoshua.com/sql-journal/rss.xml">http://www.pocketjoshua.com/sql-journal/rss.xml     <br /></a><a title="http://www.pocketjoshua.com/sql" href="http://www.pocketjoshua.com/sql">http://www.pocketjoshua.com/sql</a></p>]]></content:encoded></rss:item></rdf:RDF>
