<?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>youdidwhatwithtsql.com &#187; Computed Columns</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/computed-columns/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Computed Columns in SQL Server</title>
		<link>http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377</link>
		<comments>http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377#comments</comments>
		<pubDate>Sat, 26 Sep 2009 19:33:32 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Computed Columns]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377</guid>
		<description><![CDATA[So exactly what is a computed column? MSDN has this to say A computed column is computed from an expression that can use other columns in the same table. The expression can be a noncomputed column name, constant, function, and any combination of these connected by one or more operators. The expression cannot be a [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377">Computed Columns in SQL Server</a></p>
]]></description>
			<content:encoded><![CDATA[<p>So exactly what is a computed column? <a href="http://msdn.microsoft.com/en-us/library/ms191250.aspx" target="_blank">MSDN</a> has this to say</p>
<blockquote><p>A computed column is computed from an expression that can use other columns in the same table. The expression can be a noncomputed column name, constant, function, and any combination of these connected by one or more operators. The expression cannot be a subquery.</p>
</blockquote>
<p>Essentially there are two types of computed columns; a virtual column where the data is not physically stored in the table, values are calculated each time it is referenced in a query. These cannot be indexed. By making use of the PERSISTED TSQL keyword we can force the database engine to physically store this data in the table. These can be indexed provided the computation definition is deterministic. For example, we wouldn’t be permitted to index columns containing a call to the <a href="http://msdn.microsoft.com/en-us/library/ms188383.aspx" target="_blank">GETDATE</a> function.</p>
<p>Some people consider virtual computed columns to be a <a href="http://forums.mysql.com/read.php?10,218549,218555#msg-218555" target="_blank">waste of time</a>. Sure, it’s easy enough to build something like monthPay * 12 AS YearlySalary into your queries but in my opinion they offer the following benefits.</p>
<ul>
<li>Enforce a particular calculation method in your organisation. </li>
<li>Should the calculation need to change, modify the computed column definition and not loads of queries, procedures and reports. (Anyone having to cope with the UK’s <a href="http://www.hmrc.gov.uk/pbr2008/measure1.htm" target="_blank">change in VAT</a> last year will appreciate this). </li>
</ul>
<p>I’ve put persisted computed columns to good use recently. Some data came in from a client in an unexpected format. The record key value was sometimes contained in a column that was basically a <a href="http://en.wikipedia.org/wiki/Hodge-podge" target="_blank">hodge-podge</a> of notes. I created a computed column that extracted this value which then allowed it to be indexed. </p>
<p>Here’s an brief example of what I did to achieve this.</p>
<p>The code here uses SQL Server 2008. Firstly I created a <a href="http://msdn.microsoft.com/en-us/library/ms186755.aspx" target="_blank">user-defined function</a> that would extract the required key value from a note field.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p377code5'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3775"><td class="code" id="p377code5"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- This function will provide the computed column definition</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">FUNCTION</span> udf_extractPersonCode
<span style="color: #808080;">&#40;</span>
	@<span style="color: #0000FF;">input</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">RETURNS</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">WITH</span> SCHEMAB<span style="color: #808080;">IN</span>D<span style="color: #808080;">IN</span>G
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #0000FF;">DECLARE</span> @extractedPersonCode <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span>;
&nbsp;
	<span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">PATINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'%[A-Z][0-9][0-9][0-9][0-9][0-9][0-9]%'</span>, @<span style="color: #0000FF;">input</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">BEGIN</span>
		<span style="color: #0000FF;">SET</span> @extractedPersonCode <span style="color: #808080;">=</span> <span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">input</span>, <span style="color: #FF00FF;">PATINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'%[A-Z][0-9][0-9][0-9][0-9][0-9][0-9]%'</span>, @<span style="color: #0000FF;">input</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">7</span><span style="color: #808080;">&#41;</span>;
	<span style="color: #0000FF;">END</span>
&nbsp;
	<span style="color: #0000FF;">RETURN</span> @extractedPersonCode;
&nbsp;
<span style="color: #0000FF;">END</span>
GO</pre></td></tr></table></div>

<p>The WITH SCHEMABINDING is needed in the function definition or the following error will occur when attempting to create the function.</p>
<pre>Msg 4936, Level 16, State 1, Line 1
Computed column 'ExtractedPersonCode' in table 'ComputedColTable' cannot be persisted because the column is non-deterministic.</pre>
<p>Create this test table in the same database as the function. Note the definition of the computed column <strong>ExtractedPersonCode</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p377code6'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3776"><td class="code" id="p377code6"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">ComputedColTable</span>
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	PersonCode <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">7</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	Notes <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	ExtractedPersonCode <span style="color: #0000FF;">AS</span> dbo.<span style="color: #202020;">udf_extractPersonCode</span><span style="color: #808080;">&#40;</span>Notes<span style="color: #808080;">&#41;</span> PERSISTED
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Now insert some test data.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p377code7'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3777"><td class="code" id="p377code7"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Insert some sample data into our table</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">ComputedColTable</span>
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	PersonCode,
	Notes
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Rhys'</span>,
	<span style="color: #FF0000;">'Campbell'</span>,
	<span style="color: #FF0000;">'C123456'</span>, <span style="color: #008080;">-- Correctly entered PersonCode</span>
	<span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'John'</span>,
	<span style="color: #FF0000;">'Smith'</span>,
	<span style="color: #808080;">NULL</span>, <span style="color: #008080;">-- No Person Code here</span>
	<span style="color: #FF0000;">'Person code = C654321 New Customer'</span> <span style="color: #008080;">-- PersonCode put in notes</span>
<span style="color: #808080;">&#41;</span>,
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Joe'</span>,
	<span style="color: #FF0000;">'Bloggs'</span>,
	<span style="color: #808080;">NULL</span>, <span style="color: #008080;">-- No Person Code here</span>
	<span style="color: #FF0000;">'Visited London store C654320 customer code.'</span> <span style="color: #008080;">-- PersonCode put in notes</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Now lets view the data.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p377code8'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3778"><td class="code" id="p377code8"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Select data</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">ComputedColTable</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/computed_column_sql_server.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="computed_column_sql_server" border="0" alt="computed column sql server thumb Computed Columns in SQL Server" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/computed_column_sql_server_thumb.png" width="644" height="111" /></a> </p>
<p>Now we have cleaner data without any manual intervention needed.</p>
<p><map name='google_ad_map_377_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/377?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_377_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=377&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcomputed-columns-in-sql-server%2F377' title="Computed Columns in SQL Server" alt=" Computed Columns in SQL Server" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377">Computed Columns in SQL Server</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/computed-columns-in-sql-server/377/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

