<?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>Never Say Never &#187; String Manipulation</title>
	<atom:link href="http://phelabaum.com/archive/tag/string-manipulation/feed/" rel="self" type="application/rss+xml" />
	<link>http://phelabaum.com</link>
	<description>MS SQL Server Development</description>
	<lastBuildDate>Thu, 29 Sep 2011 02:20:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Tally Table &#8211; String Cleaning</title>
		<link>http://phelabaum.com/archive/2010/01/tally-table-string-cleaning/</link>
		<comments>http://phelabaum.com/archive/2010/01/tally-table-string-cleaning/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 14:37:11 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[String Manipulation]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Tally Table]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/01/tally-table-string-cleaning/</guid>
		<description><![CDATA[Overview This is something that most people eventually need for reporting purposes.  This function uses a Tally table to &#8216;clean&#8217; a string, removing anything you don&#8217;t specify in the @Rep parameter.  In the case below, I remove everything but letters, numbers, spaces, commas and periods.   This is a slightly modified version of a function [...]]]></description>
			<content:encoded><![CDATA[<p><span style="text-decoration: underline;"><span style="font-size: small;"> </span></span></p>
<p><span style="text-decoration: underline;"><span style="font-size: small;">Overview</span></span><br />
This is something that most people eventually need for reporting purposes.  This function uses a Tally table to &#8216;clean&#8217; a string, removing anything you don&#8217;t specify in the @Rep parameter.  In the case below, I remove everything but letters, numbers, spaces, commas and periods.   This is a slightly modified version of a function I wrote for something else, so the specifics like replacing a lot of the double spaces, and replacing carriage returns and line breaks with spaces were specifically for that.  Remove those pieces of code if you wish, or add those as additional parameters to the function.</p>
<p><span style="text-decoration: underline;"><span style="font-size: small;">Other Methods</span></span><br />
I&#8217;ll mention that I&#8217;ve seen people claim a while loop will beat this method and others that say this method is still faster.  I haven&#8217;t tested this enough personally to make the claim one way or another, but I prefer this one.  This is also one of the accepted places where a CLR function can be superior to T-SQL, but for people who can&#8217;t or don&#8217;t want to use CLR on their servers, this will still work.</p>
<p><span style="text-decoration: underline;"><span style="font-size: small;"> </span></span></p>
<p><span style="text-decoration: underline;"><span style="font-size: small;">Tally Method</span></span></p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* <br />
SELECT dbo.SSC_fn_TallyClean('This will remove all of this----&gt;!)#%*)^+__#@#$+_!)#~!!! &lt;----.','[a-zA-Z0-9. ,]') Cleaned <br />
*/</span> <br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> SSC_fn_TallyClean<span style="color: #66cc66;">&#40;</span> <br />
&nbsp; &nbsp; @A <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; @Rep <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span> <br />
<br />
<span style="color: #993333; font-weight: bold;">AS</span> <br />
<span style="color: #993333; font-weight: bold;">BEGIN</span> <br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @B <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #993333; font-weight: bold;">SET</span> @B <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">''</span> <span style="color: #808080; font-style: italic;">-- Initialize @B</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- Remove Line Feed / Carriage Returns (The tally code would have removed them, but I wanted to replace them with spaces for readability.)</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @A <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>@A<span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">13</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span> <br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @B <span style="color: #66cc66;">=</span> @B <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@A<span style="color: #66cc66;">,</span>N<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> Tally <br />
<span style="color: #993333; font-weight: bold;">WHERE</span> N <span style="color: #66cc66;">&lt;=</span> DATALENGTH<span style="color: #66cc66;">&#40;</span>@A<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@A<span style="color: #66cc66;">,</span>N<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">LIKE</span> @Rep <span style="color: #808080; font-style: italic;">-- Remove everything but letters, numbers, spaces, period and comma. </span><br />
<br />
<span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>@B<span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- Removes some double spaces. </span><br />
<span style="color: #993333; font-weight: bold;">END</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/01/tally-table-string-cleaning/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String Manipulation &#8211; CHARINDEX()</title>
		<link>http://phelabaum.com/archive/2009/12/string-manipulation-charindex/</link>
		<comments>http://phelabaum.com/archive/2009/12/string-manipulation-charindex/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 18:34:22 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[charindex]]></category>
		<category><![CDATA[String Manipulation]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2009/12/string-manipulation-charindex/</guid>
		<description><![CDATA[I see charindex used quite commonly in string manipulation.&#160; What I rarely see used is the optional third parameter.&#160; Here I will explain how to use it, a situation where it can be useful and something to be careful of. FROM BOL: Syntax CHARINDEX ( expression1 ,expression2 [ , start_location ] ) Arguments expression1 Is [...]]]></description>
			<content:encoded><![CDATA[<p>I see charindex used quite commonly in string manipulation.&#160; What I rarely see used is the optional third parameter.&#160; Here I will explain how to use it, a situation where it can be useful and something to be careful of.</p>
<blockquote><p><strong><font size="1">FROM BOL:          <br /></font><span style="font-size: small">Syntax</span></strong>       <br />CHARINDEX ( expression1 ,expression2 [ , start_location ] )</p>
<p><strong><span style="font-size: small">Arguments</span></strong>       <br /><strong>expression1        <br /></strong>Is a character expression that contains the sequence to be found. expression1 is limited to 8000 characters.</p>
<p><strong>expression2 </strong>      <br />Is a character expression to be searched.</p>
<p><strong>start_location</strong>       <br />Is an integer or bigint expression at which the search starts. If start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expression2.</p>
</blockquote>
<p><strong>How it works      <br /></strong>Let&#8217;s say we have a string with a set of characters in it and you want to find&#160; what is in between them.&#160; Easy enough if they&#8217;re different characters like [] or () (You could still use this for that situation to ensure you didn&#8217;t get an ending char before your starting char, and the warning below is still valid).&#160; But what if they&#8217;re the same character such as &quot;&quot; or &#8211;?&#160; Here&#8217;s how to use the third parameter of charindex along with a substring to accomplish this.</p>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<pre class="csharpcode"><span class="rem">-- Create a sample string with a pair of quotes.  The goal is to get what is in between the quotes. </span>
<span class="kwrd">declare</span> @a <span class="kwrd">varchar</span>(50)
<span class="kwrd">SET</span> @a = <span class="str">'This is a &quot;test&quot; string.'</span> 

<span class="rem">-- Find the First &quot; in the string </span>
<span class="kwrd">SELECT</span> <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)
<span class="rem">-- Find the Second &quot; in the string by starting one character past the first one </span>
<span class="kwrd">SELECT</span> <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a,<span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1)
<span class="rem">-- Put it together </span>
<span class="kwrd">SELECT</span> <span class="kwrd"><span style="color: #ff00ff">SUBSTRING</span></span>(@a,
                <span class="rem">-- Start from first &quot; </span>
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a),
                <span class="rem">-- Position of the second &quot; minus the position of the first &quot; to find length. </span>
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a,<span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1) - <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a))</pre>
<p>
  <br />We&#8217;re almost there.&#160; If you run the code, you&#8217;ll notice that the result is &quot;test .&#160; Let&#8217;s trim off that extra &quot;. </p>
<p></p>
<pre class="csharpcode"><span class="rem">-- Clean it up </span>
<span class="kwrd">SELECT</span> <span class="kwrd"><span style="color: #ff00ff">SUBSTRING</span></span>(@a,
                <span class="rem">-- Add 1 to trim off beginning quote </span>
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1,
                <span class="rem">-- Subtract 1 to trim off ending quote                            </span>
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a,<span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1)-1 - <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a))</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p><strong>Caution </strong></p>
<p>This is really a caution of the substring function, not charindex, but it is something that will cause the above technique to fail (and error).&#160; The problem occurs when you do not find the characters that you&#8217;re looking for.&#160; In the above example, I was looking for two &quot;&#8217;s.&#160; If they don&#8217;t exist, the code will error out.&#160; If no quotes are present, it works up until the cleanup portion.&#160; If only one &quot; is present, it fails at the first substring.&#160; You will receive this error:</p>
<p><span style="color: #ff0000">Msg 537, Level 16, State 2, Line 20</span> </p>
<p><font color="#ff0000">Invalid length parameter passed to the LEFT or SUBSTRING function.</font></p>
</p>
<p>The reason for this is that it attempts to pass a negative length to the substring function, which isn&#8217;t allowed.&#160; To safeguard against this, you can use a case statement like the following:</p>
<pre class="csharpcode"><span class="kwrd">DECLARE</span> @a <span class="kwrd">varchar</span>(50)
<span class="kwrd">SET</span> @a = <span class="str">'This is a &quot;fail  string.'</span> 

                <span class="rem">-- Test for a second &quot; before attempting the substring. </span>
<span class="kwrd">SELECT</span> <span class="kwrd">CASE</span> <span class="kwrd">WHEN</span> <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a,<span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1)-1 - <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)) &gt; 0 <span class="kwrd">THEN</span>
        <span class="kwrd"><span style="color: #ff00ff">SUBSTRING</span></span>(@a,
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1,
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a,<span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1)-1 - <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a))
            <span class="kwrd">ELSE</span> <span class="str">'Not Found'</span>
            <span class="kwrd">End</span></pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2009/12/string-manipulation-charindex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tally Table &#8211; Character Date Validation</title>
		<link>http://phelabaum.com/archive/2009/12/tally-table-character-date-validation/</link>
		<comments>http://phelabaum.com/archive/2009/12/tally-table-character-date-validation/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 00:34:48 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Date Manipulation]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[String Manipulation]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Tally Table]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2009/12/tally-table-character-date-validation/</guid>
		<description><![CDATA[Tally (or numbers) tables are one of my favorite query writing tools.  Such a simple premise that can be applied to a wide variety of problems.   If you don&#8217;t know what they are, I&#8217;d recommend this article.   Here is one problem I&#8217;ve seen a few times now that you can use a Tally table to [...]]]></description>
			<content:encoded><![CDATA[<p>Tally (or numbers) tables are one of my favorite query writing tools.  Such a simple premise that can be applied to a wide variety of problems.   If you don&#8217;t know what they are, I&#8217;d recommend <a href="http://www.sqlservercentral.com/articles/T-SQL/62867/" target="_blank">this article</a>.   Here is one problem I&#8217;ve seen a few times now that you can use a Tally table to easily solve.  This is by no means the only way to do something like this, nor is it necessarily even the *best* way, but it is *a* way.</p>
<p><span style="text-decoration: underline;">The Problem:</span></p>
<p>You have a char/varchar column that holds dates in YYYYMMDD format.  You now need to cast these values to dates.  The problem is, with no validation on the column, some of these values aren&#8217;t actually valid dates and you get conversion errors.  You&#8217;d like to go through and identify anything that didn&#8217;t follow the pattern of YYYYMMDD or wasn&#8217;t a valid date so that it can be either fixed or removed.</p>
<p><span style="text-decoration: underline;">The Tally Solution:</span></p>
<p>The following query uses a Tally table to generate a range of valid dates that you can check your varchar field against.  The below version works in 2000+.  (You can just as easily use a CTE tally in 2005/2008)</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @StartDate datetime<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @EndDate datetime<br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> @StartDate <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'19910101 00:00:00'</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @EndDate <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'20250101 00:00:00'</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> YourTable<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> vcdatecolumn <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">&#40;</span>dateadd<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">,</span>n<span style="color: #66cc66;">,</span>@StartDate<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span><br />
<span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'0'</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">&#40;</span>dateadd<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">,</span>n<span style="color: #66cc66;">,</span>@StartDate<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span><br />
<span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'0'</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">&#40;</span>dateadd<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">,</span>n<span style="color: #66cc66;">,</span>@StartDate<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> Tally<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> DATEADD<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">,</span>n<span style="color: #66cc66;">,</span>@StartDate<span style="color: #66cc66;">&#41;</span> &amp;lt;<span style="color: #66cc66;">=</span> @EndDate<br />
<span style="color: #66cc66;">&#41;</span></div></div>
<p><span style="text-decoration: underline;">Other Methods:</span><br />
As mentioned, there are a lot of ways to do this.  Because ISDATE() jumps out as being such an easy one, I&#8217;ll mention a couple of the problems with using that method.  The main gap left is that some of the junk values (fairly common with this setup) like &#8217;9901&#8242; are valid dates according to ISDATE().  However, it is unlikely that this date is really supposed to be 9901/01/01.  You can add a length check, but then you run into &#8217;9901June&#8217; still being valid, etc. etc.  Using the Tally method, all of these are simply flagged as invalid and you can make up your mind about what to do with them once they&#8217;ve been identified.</p>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2009/12/tally-table-character-date-validation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

