<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Jeremy Coenen</title>
	
	<link>http://jeremycoenen.com</link>
	<description />
	<lastBuildDate>Fri, 05 Jun 2009 16:50:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/JeremyCoenen" /><feedburner:info uri="jeremycoenen" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly>This is an XML content feed. It is intended to be viewed in a newsreader or syndicated to another site, subject to copyright and fair use.</feedburner:browserFriendly><item>
		<title>Get word count from SQL Server table</title>
		<link>http://feedproxy.google.com/~r/JeremyCoenen/~3/5r0Wner_GMQ/get-word-count-from-sql-server-table</link>
		<comments>http://jeremycoenen.com/sqlserver/get-word-count-from-sql-server-table#comments</comments>
		<pubDate>Fri, 05 Jun 2009 16:50:12 +0000</pubDate>
		<dc:creator>Jeremy Coenen</dc:creator>
				<category><![CDATA[SQLServer]]></category>

		<guid isPermaLink="false">http://jeremycoenen.com/?p=5</guid>
		<description><![CDATA[For one of my projects I needed to go into the database and get a rough estimate of the number of words that needed to be translated from various tables in our database. I looked around to see if anyone had come up with some t-sql to do this, but did not find much in [...]]]></description>
				<content:encoded><![CDATA[<p>For one of my projects I needed to go into the database and get a rough estimate of the number of words that needed to be translated from various tables in our database.  </p>
<p>I looked around to see if anyone had come up with some t-sql to do this, but did not find much in my initial search so I ended up creating this rudimentary stored procedure.  </p>
<pre class="tsql"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> GetWordCount <span style="color: #808080;">&#40;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    @tableName <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>,</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">    @columnName <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000FF;">AS</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">SQL</span> <span style="color: #0000FF;">NVARCHAR</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4000</span><span style="color: #808080;">&#41;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000FF;">SELECT</span> @<span style="color: #0000FF;">SQL</span> = <span style="color: #FF0000;">'SELECT SUM( (1 + DATALENGTH('</span> +@columnName +<span style="color: #FF0000;">') -</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;"> DATALENGTH(REPLACE(CAST('</span>+ @columnName + <span style="color: #FF0000;">'</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;"> AS NVARCHAR(MAX)), '</span><span style="color: #FF0000;">' '</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">')))) AS WordCount </span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">FROM '</span> + @tablename</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_EXECUTESQL</span> @<span style="color: #0000FF;">SQL</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p>I'm sure there are flaws in the design (basically counting spaces), but it gets me a close enough estimate.   </p>
<p>Note: I ended up using DataLength() over LEN() to be able to hand text/ntext fields and also casted the column as NVARCHAR(MAX) for the very same reason.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JeremyCoenen?a=5r0Wner_GMQ:xTeY6GrO4XM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/JeremyCoenen?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeremyCoenen?a=5r0Wner_GMQ:xTeY6GrO4XM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JeremyCoenen?i=5r0Wner_GMQ:xTeY6GrO4XM:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeremyCoenen?a=5r0Wner_GMQ:xTeY6GrO4XM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JeremyCoenen?i=5r0Wner_GMQ:xTeY6GrO4XM:V_sGLiPBpWU" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://jeremycoenen.com/sqlserver/get-word-count-from-sql-server-table/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jeremycoenen.com/sqlserver/get-word-count-from-sql-server-table</feedburner:origLink></item>
	</channel>
</rss>
