<?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; Tally Table</title>
	<atom:link href="http://phelabaum.com/archive/tag/tally-table/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 CTE</title>
		<link>http://phelabaum.com/archive/2010/03/tally-table-cte/</link>
		<comments>http://phelabaum.com/archive/2010/03/tally-table-cte/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 13:24:48 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Tally Table]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/03/tally-table-cte/</guid>
		<description><![CDATA[Now that I have several posts on what you can do with a Tally table, I figured I&#8217;d share my favorite way to create one inline.  I still prefer to have a physical tally table (usually in a Utility database that can be accessed from anywhere and doesn&#8217;t need to be created in each individual [...]]]></description>
			<content:encoded><![CDATA[<p>Now that I have several posts on what you can do with a Tally table, I figured I&#8217;d share my favorite way to create one inline.  I still prefer to have a physical tally table (usually in a Utility database that can be accessed from anywhere and doesn&#8217;t need to be created in each individual database) for permament code, but for times when you need one on the fly, this is my preferred method.  I can&#8217;t really take the credit for this query, the base construct is based on something I&#8217;ve seen attributed to Itzik Ben-Gan.   I&#8217;ve modified it a bit and changed up the formatting to be the way I like it.  Anything over a few thousand rows I&#8217;d probably use a physical tally table for, but on small numbers you shouldn&#8217;t see much of a performance hit with this script.</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: #808080; font-style: italic;">-- Tally Table CTE script (SQL 2005+ only)</span><br />
<span style="color: #808080; font-style: italic;">-- You can use this to create many different numbers of rows... for example:</span><br />
<span style="color: #808080; font-style: italic;">-- You could use a 3 way cross join (t3 x, t3 y, t3 z) instead of just 2 way to generate a different number of rows.</span><br />
<span style="color: #808080; font-style: italic;">-- The # of rows this would generate for each is noted in the X3 comment column below.</span><br />
<span style="color: #808080; font-style: italic;">-- For most common usage, I find t3 or t4 to be enough, so that is what is coded here.</span><br />
<span style="color: #808080; font-style: italic;">-- If you use t3 in ‘Tally’, you can delete t4 and t5.</span><br />
<br />
; <span style="color: #993333; font-weight: bold;">WITH</span><br />
<span style="color: #808080; font-style: italic;">-- Tally table Gen            Tally Rows:     X2                X3</span><br />
t1 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>    <span style="color: #808080; font-style: italic;">-- 4            ,    8</span><br />
t2 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N <span style="color: #993333; font-weight: bold;">FROM</span> t1 x<span style="color: #66cc66;">,</span> t1 y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>            <span style="color: #808080; font-style: italic;">-- 16            ,    64</span><br />
t3 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N <span style="color: #993333; font-weight: bold;">FROM</span> t2 x<span style="color: #66cc66;">,</span> t2 y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>            <span style="color: #808080; font-style: italic;">-- 256            ,    4096</span><br />
t4 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N <span style="color: #993333; font-weight: bold;">FROM</span> t3 x<span style="color: #66cc66;">,</span> t3 y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>            <span style="color: #808080; font-style: italic;">-- 65536        ,    16,777,216</span><br />
t5 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N <span style="color: #993333; font-weight: bold;">FROM</span> t4 x<span style="color: #66cc66;">,</span> t4 y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>            <span style="color: #808080; font-style: italic;">-- 4,294,967,296,    A lot</span><br />
Tally <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">ROW_NUMBER</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> N<br />
<span style="color: #993333; font-weight: bold;">FROM</span> t3 x<span style="color: #66cc66;">,</span> t3 y<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- Change the t3's to one of the other numbers above for more/less rows</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/03/tally-table-cte/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tally Table &#8211; Delimited list to Table</title>
		<link>http://phelabaum.com/archive/2010/02/tally-table-delimited-list-to-table/</link>
		<comments>http://phelabaum.com/archive/2010/02/tally-table-delimited-list-to-table/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 20:49:27 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Tally Table]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/02/tally-table-delimited-list-to-table/</guid>
		<description><![CDATA[Dealing with delimited lists (Usually separated by a comma) in SQL is a problem easily handled by a simple function and a Tally Table.  (Tally tables are also often referred to as Numbers tables or spt_values tables.  If you still don&#8217;t know what that is, please see this excellent article on Tally tables written by [...]]]></description>
			<content:encoded><![CDATA[<p>Dealing with delimited lists (Usually separated by a comma) in SQL is a problem easily handled by a simple function and a Tally Table.  (Tally tables are also often referred to as Numbers tables or spt_values tables.  If you still don&#8217;t know what that is, please see this <a href="http://www.sqlservercentral.com/articles/T-SQL/62867/" target="_blank"><span style="text-decoration: underline;">excellent article</span></a> on Tally tables written by my friend and SSC heavyweight <a href="http://www.sqlservercentral.com/Authors/Articles/Jeff_Moden/80567/" target="_blank">Jeff Moden</a>.)  This particular implementation is somewhat specific in nature but can give you an alternative to Dynamic SQL when you want to pass in a list as a parameter and do an IN in a Stored Procedure. The following function will take your delimiter and string and parse it into a table so you can do your IN.  (I&#8217;m leaving my standard header on the function in this case because there are some good notes in there.)</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 />
=============================================================================================<br />
CREATE DATE:     02/27/2010<br />
LAST MODIFIED:    02/27/2010<br />
CREATED BY:        SETH PHELABAUM<br />
PURPOSE:        Splits a string based on a passed in delimiter and returns a table.<br />
ISSUES:            Strings with extra 's will break this function, handle that on the end that calls it.<br />
Notes:            To make it a simpler function, I removed the peice that trimmed spaces around commas.  Do<br />
this before or after calling it.<br />
Revision History:<br />
Date     By        Change Made<br />
-------- ---      -------------------------------------<br />
=============================================================================================<br />
GRANT SELECT ON TVF_TallySplit TO [Somebody]<br />
SELECT * FROM TVF_TallySplit(',','Orange,Apple,Banana,Pear,Watermelon,Grape')<br />
SELECT * FROM TVF_TallySplit('*','Orange*Apple*Banana*Pear*Watermelon*Grape')<br />
DROP FUNCTION TVF_TallySplit<br />
*/</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> TVF_TallySplit<span style="color: #66cc66;">&#40;</span><br />
@Delim            <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>            <span style="color: #808080; font-style: italic;">-- List Delimiter</span><br />
@String            <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8000</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;">TABLE</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<br />
<span style="color: #993333; font-weight: bold;">RETURN</span><span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@Delim <span style="color: #66cc66;">+</span> @String <span style="color: #66cc66;">+</span> @Delim<span style="color: #66cc66;">,</span>N<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>CHARINDEX<span style="color: #66cc66;">&#40;</span>@Delim<span style="color: #66cc66;">,</span>@Delim <span style="color: #66cc66;">+</span> @String <span style="color: #66cc66;">+</span> @Delim<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: #66cc66;">-</span>N<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> ListValue<br />
<span style="color: #993333; font-weight: bold;">FROM</span> Tally<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> N &amp;lt; LEN<span style="color: #66cc66;">&#40;</span>@Delim <span style="color: #66cc66;">+</span> @String <span style="color: #66cc66;">+</span> @Delim<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@Delim <span style="color: #66cc66;">+</span> @String <span style="color: #66cc66;">+</span> @Delim<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: #66cc66;">=</span> @Delim <span style="color: #66cc66;">&#41;</span></div></div>
<p><strong><span style="text-decoration: underline;">What to do with this</span></strong><br />
Let&#8217;s say you have a table containing names of your favorite fruits.  (In case you were wondering&#8230; No, these aren&#8217;t my favorite fruits; they were just ones that immediately came to mind when writing this.  I don&#8217;t even like half of these.)</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;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Fruits<span style="color: #66cc66;">&#40;</span><br />
Name        <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> Fruits<span style="color: #66cc66;">&#40;</span>Name<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Apple'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Banana'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Grapefruit'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Kiwi'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span><span style="color: #ff0000;">'Tomatoe'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Grape'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Orange'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'DragonFruit'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Strawberry'</span></div></div>
<p>You then ask someone else what their favorite fruits are and want to see what fruits you have in common.  You might think you could just write a query for that like this:</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> @YFFruits <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> @YFFruits <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Orange,Apple,Banana,Pear,Watermelon,Grape'</span><br />
<span style="color: #808080; font-style: italic;">-- OR SET @YFFruits = '''Orange'',''Apple'',''Banana'',''Pear'',''Watermelon'',''Grape'''</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Fruits <span style="color: #993333; font-weight: bold;">WHERE</span> Name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #66cc66;">&#40;</span>@YFFruits<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">-- OR SELECT * FROM Fruits where Name IN (@YFFruits)</span></div></div>
<p>However, this won&#8217;t work because in all these cases SQL is looking for a single fruit named : &#8216;Orange,Apple,Banana,Pear,Watermelon,Grape&#8217; not any fruit in what is really a list.</p>
<p>A common solution for this is to use Dynamic SQL which would make your query this:</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> @YFFruits <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @YFFruits <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">''</span><span style="color: #ff0000;">'Orange'</span><span style="color: #ff0000;">','</span><span style="color: #ff0000;">'Apple'</span><span style="color: #ff0000;">','</span><span style="color: #ff0000;">'Banana'</span><span style="color: #ff0000;">','</span><span style="color: #ff0000;">'Pear'</span><span style="color: #ff0000;">','</span><span style="color: #ff0000;">'Watermelon'</span><span style="color: #ff0000;">','</span><span style="color: #ff0000;">'Grape'</span><span style="color: #ff0000;">''</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'SELECT * FROM Fruits WHERE Name IN ('</span> <span style="color: #66cc66;">+</span> @YFFruits <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">')'</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>This works and will properly match up your fruits.</p>
<p>The above function allows you to accomplish your goal without Dynamic SQL with a query that looks like this:</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> @YFFruits <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @YFFruits <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Orange,Apple,Banana,Pear,Watermelon,Grape'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Fruits <span style="color: #993333; font-weight: bold;">WHERE</span> Name <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Util<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>TVF_TallySplit<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">','</span><span style="color: #66cc66;">,</span>@YFFruits<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>You can also simply join to the table instead of using the IN keyword which gives you more flexibility in your query writing.</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> @YFFruits <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @YFFruits <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Orange,Apple,Banana,Pear,Watermelon,Grape'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> Fruits<span style="color: #66cc66;">.*</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> Fruits<br />
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> Util<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>TVF_TallySplit<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">','</span><span style="color: #66cc66;">,</span>@YFFruits<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> T <span style="color: #993333; font-weight: bold;">ON</span> Fruits<span style="color: #66cc66;">.</span>Name <span style="color: #66cc66;">=</span> T<span style="color: #66cc66;">.</span>ListValue</div></div>
<p>Note that because I wanted to keep the function somewhat simple, it does not handle extra spaces around the commas.  Single quotes within the string will also break it which limits its usage somewhat.  If this is a concern for your implementation, you either need to replace the single quotes on both sides or use a different method.  Despite the fact that the example above uses a list of strings, in real life situations I use this mainly for lists of uniqueidentifiers or numbers where single quotes/spaces are never an issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/02/tally-table-delimited-list-to-table/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>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>

