<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>SQL Tips by Namwar Rizvi &#187; tips</title>
	<atom:link href="http://sqltips.wordpress.com/category/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqltips.wordpress.com</link>
	<description>SQL Tips and Tricks by Namwar Rizvi</description>
	<lastBuildDate>Mon, 31 Aug 2009 22:21:46 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='sqltips.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/ed069197b1591bd81b1bcccb593c4d5f?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>SQL Tips by Namwar Rizvi &#187; tips</title>
		<link>http://sqltips.wordpress.com</link>
	</image>
			<item>
		<title>SQL Server Client Tools Setup Fails due to MSXML 6</title>
		<link>http://sqltips.wordpress.com/2009/04/13/sql-server-client-tools-setup-fails-due-to-msxml-6/</link>
		<comments>http://sqltips.wordpress.com/2009/04/13/sql-server-client-tools-setup-fails-due-to-msxml-6/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 19:26:05 +0000</pubDate>
		<dc:creator>namwar</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[Information]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[Service Pack]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://sqltips.wordpress.com/?p=147</guid>
		<description><![CDATA[Here is the solution to fix the failed installation of SQL Server Client Tools because of MSXML 6 corrupt installation<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=147&subd=sqltips&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>If you are trying to install SQL Server 2005 client tools and continuously getting failure message because of MSXML 6 installation which already exists on your machine then it may be due to the corrupted installation of MSXML 6 which blocks the installation of client tools. You can fix it as follows:</p>
<p>Go to <strong>Control Panel-&gt; Administrative Tools-&gt;Add Remove Programs</strong> and try to remove it. If it removes smoothly then just install the Client Tools as before and you are done. </p>
<p>OR</p>
<p>If you found that you are unable to uninstall the MSXML 6 because of some wiered error then you need a tool called <strong>Windows Install Clean up </strong>from <a href="http://support.microsoft.com/kb/290301">here</a></p>
<p>Donwload it and remove the corrupt installation of MSXML 6. Once removed, you can now install the Client tools as usual.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltips.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltips.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltips.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltips.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltips.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltips.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltips.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltips.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltips.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltips.wordpress.com/147/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=147&subd=sqltips&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sqltips.wordpress.com/2009/04/13/sql-server-client-tools-setup-fails-due-to-msxml-6/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f594e19b68c1c1098c99fb65c67717f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">namwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Search all columns of all tables for a particular value</title>
		<link>http://sqltips.wordpress.com/2009/01/08/search-all-columns-of-all-tables-for-a-particular-value/</link>
		<comments>http://sqltips.wordpress.com/2009/01/08/search-all-columns-of-all-tables-for-a-particular-value/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 19:30:22 +0000</pubDate>
		<dc:creator>namwar</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[Object Search]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Query]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://sqltips.wordpress.com/?p=129</guid>
		<description><![CDATA[Here is the solution to search all columns of all tables for a given value<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=129&subd=sqltips&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div>
<p>Several times I have came across with the question that how can I search all columns of all tables in a SQL Server database for a particular value. Unfortunately, you can not perform this query directly by any already provided system table or object. </p>
<p>There are some fundamental points to remember:</p>
<p> </p>
<ol>
<li>You have to iterate the list of tables and for each table you have to iterate each column and perform an exist query against it. </li>
<li>You will need three types of queries due to the data types i.e. string, number and date and time.</li>
</ol>
<div>I have found an excellent article <a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/the-ten-most-asked-sql-server-questions--1#2">here</a> which provides a good explanation along with the necessary sql script. I would recommend every reader to read this article.</div>
<p> </p></div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltips.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltips.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltips.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltips.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltips.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltips.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltips.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltips.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltips.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltips.wordpress.com/129/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=129&subd=sqltips&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sqltips.wordpress.com/2009/01/08/search-all-columns-of-all-tables-for-a-particular-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f594e19b68c1c1098c99fb65c67717f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">namwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Generate rows for missing dates</title>
		<link>http://sqltips.wordpress.com/2008/11/10/generate-rows-for-missing-dates/</link>
		<comments>http://sqltips.wordpress.com/2008/11/10/generate-rows-for-missing-dates/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 21:16:04 +0000</pubDate>
		<dc:creator>namwar</dc:creator>
				<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[CTE]]></category>

		<guid isPermaLink="false">http://sqltips.wordpress.com/?p=117</guid>
		<description><![CDATA[Here is the quickest way to generate the rows for your missing dates<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=117&subd=sqltips&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>For all those reports or queries where you want to list a row for each day such that if no data is available for that day then you need zero against that date then you can use the Calendar table for the missing dates.</p>
<p>For generating a calendar table efficiently, you can refer to my earlier post <a title="Generating Calendar Table dynamically and efficiently" href="http://blog.namwarrizvi.com/?p=139" target="_blank">here</a></p>
<p>Assuming you have a calendar table, use the following query to generate the required result</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">Use</span> tempdb
<span class="kwrd">Go</span>

/*<span class="kwrd">Create</span> a sample <span class="kwrd">table</span>*/
<span class="kwrd">drop</span> <span class="kwrd">table</span> sampleData
<span class="kwrd">go</span>
<span class="kwrd">Create</span> <span class="kwrd">table</span> sampleData
(
    id <span class="kwrd">int</span> <span class="kwrd">IDENTITY</span>(1,1) <span class="kwrd">NOT</span> <span class="kwrd">NULL</span>,
    transactionDate datetime,
    Qty <span class="kwrd">int</span>

)

<span class="kwrd">Insert</span> <span class="kwrd">into</span> sampleData (transactionDate,Qty) <span class="kwrd">values</span> (<span class="str">'20081101'</span>,2)
<span class="kwrd">Insert</span> <span class="kwrd">into</span> sampleData (transactionDate,Qty) <span class="kwrd">values</span> (<span class="str">'20081101'</span>,3)

<span class="kwrd">Insert</span> <span class="kwrd">into</span> sampleData (transactionDate,Qty) <span class="kwrd">values</span> (<span class="str">'20081104'</span>,1)
<span class="kwrd">Insert</span> <span class="kwrd">into</span> sampleData (transactionDate,Qty) <span class="kwrd">values</span> (<span class="str">'20081104'</span>,2)
<span class="kwrd">Go</span>

<span class="kwrd">Select</span> C.CalendarDate,Isnull(A.TotalQty,0) <span class="kwrd">as</span> TotalQty
<span class="kwrd">from</span>
Calendar C
<span class="kwrd">left</span> <span class="kwrd">outer</span> <span class="kwrd">join</span>
(
    <span class="kwrd">Select</span> transactionDate,<span class="kwrd">Sum</span>(Qty) <span class="kwrd">as</span> TotalQty
    <span class="kwrd">from</span> sampleData
    <span class="kwrd">group</span> <span class="kwrd">by</span> transactionDate
) A
<span class="kwrd">on</span> C.CalendarDate=A.transactionDate
<span class="kwrd">Where</span> C.CalendarDate <span class="kwrd">between</span> <span class="str">'20081101'</span> <span class="kwrd">AND</span> <span class="str">'20081130'</span></pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltips.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltips.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltips.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltips.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltips.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltips.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltips.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltips.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltips.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltips.wordpress.com/117/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=117&subd=sqltips&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sqltips.wordpress.com/2008/11/10/generate-rows-for-missing-dates/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f594e19b68c1c1098c99fb65c67717f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">namwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Quick technique to retrieve alphanumeric values by using regular expression in TSQL</title>
		<link>http://sqltips.wordpress.com/2008/09/14/quick-technique-to-retrieve-alphanumeric-values-by-using-regular-expression-in-tsql/</link>
		<comments>http://sqltips.wordpress.com/2008/09/14/quick-technique-to-retrieve-alphanumeric-values-by-using-regular-expression-in-tsql/#comments</comments>
		<pubDate>Sun, 14 Sep 2008 00:33:18 +0000</pubDate>
		<dc:creator>namwar</dc:creator>
				<category><![CDATA[Query]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Regular Expression]]></category>

		<guid isPermaLink="false">http://sqltips.wordpress.com/?p=115</guid>
		<description><![CDATA[Here is a quick technique to retrieve alphanumeric values by using regular expression in TSQL<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=115&subd=sqltips&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>If you have a column in a database which contains numeric, alphanumeric and only aplha values and you just want to find all those rows which have alphanumeric values i.e which contains atleast one numeric alongwith anynumber of alphabets then you can use the power of Like operator and a little regular expression technique. Following is a query for <strong>Name</strong> column in <strong>Production.Product </strong>table of <strong>AdventureWorks</strong> database. This query retrieves all rows which contains alphanumeric values:</p>
<p>Use AdventureWorks</p>
<p>Go</p>
<p>Select * from Production.Product Where Name like &#8216;%[0-9]%&#8217;</p>
<p>Go</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqltips.wordpress.com/115/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqltips.wordpress.com/115/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltips.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltips.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltips.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltips.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltips.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltips.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltips.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltips.wordpress.com/115/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltips.wordpress.com/115/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltips.wordpress.com/115/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=115&subd=sqltips&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sqltips.wordpress.com/2008/09/14/quick-technique-to-retrieve-alphanumeric-values-by-using-regular-expression-in-tsql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f594e19b68c1c1098c99fb65c67717f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">namwar</media:title>
		</media:content>
	</item>
		<item>
		<title>How to copy query results With column headers?</title>
		<link>http://sqltips.wordpress.com/2008/08/27/how-to-copy-query-results-with-column-headers/</link>
		<comments>http://sqltips.wordpress.com/2008/08/27/how-to-copy-query-results-with-column-headers/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 21:56:34 +0000</pubDate>
		<dc:creator>namwar</dc:creator>
				<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Shortcuts]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://sqltips.wordpress.com/?p=109</guid>
		<description><![CDATA[If you want to copy Column headers along with the query results in SQL Server Management Studio, here is the quick solution.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=109&subd=sqltips&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>It is a very common practice that sometime you need to copy the query results from <strong>SQL Server Management Studio (SSMS)</strong> results tab to excel or any other application. The problem normally we face is that it does copy column headers and you have to write the column headers by your self. Here is the quick tip to enable column headers copying.</p>
<ol>
<li>In SSMS, open <strong>Tools</strong> menu and click <strong>Options</strong></li>
<li>Expand <strong>Query Results</strong> node and click <strong>Results to Grid</strong></li>
<li>Check<strong> Include column headers when copying or saving the results.</strong> Your screen will look similar to following:</li>
<p><a href="http://sqltips.files.wordpress.com/2008/08/resulttogridoption.jpg"><img class="size-full wp-image-110" src="http://sqltips.files.wordpress.com/2008/08/resulttogridoption.jpg?w=450&#038;h=266" alt="Results To Grid Option Screen" width="450" height="266" /></a></p>
<li>Click <strong>Save</strong> to save your settings.</li>
</ol>
<p>Now, whenever you will copy the query results, column headers will be copied too.</p>
<p>Enjoy!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqltips.wordpress.com/109/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqltips.wordpress.com/109/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltips.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltips.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltips.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltips.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltips.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltips.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltips.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltips.wordpress.com/109/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltips.wordpress.com/109/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltips.wordpress.com/109/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=109&subd=sqltips&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sqltips.wordpress.com/2008/08/27/how-to-copy-query-results-with-column-headers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f594e19b68c1c1098c99fb65c67717f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">namwar</media:title>
		</media:content>

		<media:content url="http://sqltips.files.wordpress.com/2008/08/resulttogridoption.jpg" medium="image">
			<media:title type="html">Results To Grid Option Screen</media:title>
		</media:content>
	</item>
		<item>
		<title>Deleting ununsed primary keys</title>
		<link>http://sqltips.wordpress.com/2008/08/26/deleting-ununsed-primary-keys/</link>
		<comments>http://sqltips.wordpress.com/2008/08/26/deleting-ununsed-primary-keys/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 22:35:14 +0000</pubDate>
		<dc:creator>namwar</dc:creator>
				<category><![CDATA[Query]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[How To]]></category>

		<guid isPermaLink="false">http://sqltips.wordpress.com/?p=107</guid>
		<description><![CDATA[Following script demonstrate a simple technique to delete all those rows in the master table which are not used in child table.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=107&subd=sqltips&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometimes we need to make sure that all keys in the primary key table has atleast one corresponding row in child table or in other words every key has been used atleast once as a foreign key. This is a typical scenario we face during data import where data integrity can not be guarnteed 100% due to various reason. Following script demonstrate a simple technique to delete all those rows in the master table which are not used in child table.</p>
<p><!--[if gte mso 9]&gt;  Normal 0     false false false  EN-US X-NONE X-NONE              MicrosoftInternetExplorer4              &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                            &lt;![endif]--> <!--[if gte mso 10]&gt;-->  <!--[endif]--></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Use</span><span style="font-size:10pt;font-family:&quot;"> tempdb<span style="color:gray;">;</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:green;">/* Create sample tables and insert sample data */</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Create</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">table</span> tblMaster</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:gray;">(</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span>masterId <span style="color:blue;">int</span><span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span>masterName <span style="color:blue;">varchar</span><span style="color:gray;">(</span>50<span style="color:gray;">)</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:gray;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:gray;">)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Go</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Create</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">table</span> tblChild</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:gray;">(</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span>childId <span style="color:blue;">int</span> <span style="color:gray;">NOT</span> <span style="color:gray;">NULL</span> <span style="color:blue;">IDENTITY</span> <span style="color:gray;">(</span>1<span style="color:gray;">,</span> 1<span style="color:gray;">),</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span>masterId <span style="color:blue;">int</span><span style="color:gray;">,</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span>childName <span style="color:blue;">varchar</span><span style="color:gray;">(</span>50<span style="color:gray;">)</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:gray;">)</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Go</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> tblMaster <span style="color:gray;">(</span>masterId<span style="color:gray;">,</span>masterName<span style="color:gray;">)</span> <span style="color:blue;">values</span> <span style="color:gray;">(</span>1<span style="color:gray;">,</span><span style="color:red;">&#8216;Master 1&#8242;</span><span style="color:gray;">);</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> tblMaster <span style="color:gray;">(</span>masterId<span style="color:gray;">,</span>masterName<span style="color:gray;">)</span> <span style="color:blue;">values</span> <span style="color:gray;">(</span>2<span style="color:gray;">,</span><span style="color:red;">&#8216;Master 2&#8242;</span><span style="color:gray;">);</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> tblMaster <span style="color:gray;">(</span>masterId<span style="color:gray;">,</span>masterName<span style="color:gray;">)</span> <span style="color:blue;">values</span> <span style="color:gray;">(</span>3<span style="color:gray;">,</span><span style="color:red;">&#8216;Master 3&#8242;</span><span style="color:gray;">);</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> tblMaster <span style="color:gray;">(</span>masterId<span style="color:gray;">,</span>masterName<span style="color:gray;">)</span> <span style="color:blue;">values</span> <span style="color:gray;">(</span>4<span style="color:gray;">,</span><span style="color:red;">&#8216;Master 4&#8242;</span><span style="color:gray;">);</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Go</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> tblChild <span style="color:gray;">(</span>masterId<span style="color:gray;">,</span>childName<span style="color:gray;">)</span> <span style="color:blue;">values</span> <span style="color:gray;">(</span>1<span style="color:gray;">,</span><span style="color:red;">&#8216;Child 1&#8242;</span><span style="color:gray;">);</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> tblChild <span style="color:gray;">(</span>masterId<span style="color:gray;">,</span>childName<span style="color:gray;">)</span> <span style="color:blue;">values</span> <span style="color:gray;">(</span>1<span style="color:gray;">,</span><span style="color:red;">&#8216;Child 2&#8242;</span><span style="color:gray;">);</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> tblChild <span style="color:gray;">(</span>masterId<span style="color:gray;">,</span>childName<span style="color:gray;">)</span> <span style="color:blue;">values</span> <span style="color:gray;">(</span>1<span style="color:gray;">,</span><span style="color:red;">&#8216;Child 3&#8242;</span><span style="color:gray;">);</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> tblChild <span style="color:gray;">(</span>masterId<span style="color:gray;">,</span>childName<span style="color:gray;">)</span> <span style="color:blue;">values</span> <span style="color:gray;">(</span>2<span style="color:gray;">,</span><span style="color:red;">&#8216;Child 4&#8242;</span><span style="color:gray;">);</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Go</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:green;">/* Master table before deletion */</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Select</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:gray;">*</span> <span style="color:blue;">from</span> tblMaster</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Go</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:green;">/* Delete unused rows */</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Delete</span><span style="font-size:10pt;font-family:&quot;"> tblMaster</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">where</span><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:gray;">not</span> <span style="color:gray;">exists</span> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:gray;">(</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">Select</span> masterId </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">from</span> tblChild </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">where</span> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span>tblChild<span style="color:gray;">.</span>masterId<span style="color:gray;">=</span>tblMaster<span style="color:gray;">.</span>masterId</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:gray;">)</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Go</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:green;">/* Master table after deletion */</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">Select</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:gray;">*</span> <span style="color:blue;">from</span> tblMaster</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Go</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:green;">/* Clean up */</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">drop</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">table</span> tblMaster<span style="color:gray;">;</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Go</span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;color:blue;">drop</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">table</span> tblChild<span style="color:gray;">;</span></span></p>
<p class="MsoNormal" style="margin-bottom:0.0001pt;line-height:normal;"><span style="font-size:10pt;font-family:&quot;">Go</span></p>
<p class="MsoNormal">
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqltips.wordpress.com/107/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqltips.wordpress.com/107/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltips.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltips.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltips.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltips.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltips.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltips.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltips.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltips.wordpress.com/107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltips.wordpress.com/107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltips.wordpress.com/107/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=107&subd=sqltips&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sqltips.wordpress.com/2008/08/26/deleting-ununsed-primary-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f594e19b68c1c1098c99fb65c67717f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">namwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Validating Inputs and Finding Patterns with PatIndex</title>
		<link>http://sqltips.wordpress.com/2007/09/05/validating-inputs-and-finding-patterns-with-patindex/</link>
		<comments>http://sqltips.wordpress.com/2007/09/05/validating-inputs-and-finding-patterns-with-patindex/#comments</comments>
		<pubDate>Wed, 05 Sep 2007 20:54:00 +0000</pubDate>
		<dc:creator>namwar</dc:creator>
				<category><![CDATA[TSQL]]></category>
		<category><![CDATA[Utility Functions]]></category>
		<category><![CDATA[string manipulation]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://sqltips.wordpress.com/2007/09/05/validating-inputs-and-finding-patterns-with-patindex/</guid>
		<description><![CDATA[PatIndex is one of most powerful functions of TSQL but often developers ignore it firstly because of the availability of CharIndex and secondly, the difficulty of creating patterns for PatIndex to search. Developers try to solve some typical problems faced by today&#8217;s web application by developing their own workarounds instead of using PatIndex.Today, I will [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=92&subd=sqltips&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><span style="font-weight:bold;">PatIndex </span>is one of most powerful functions of TSQL but often developers ignore it firstly because of the availability of <span style="font-weight:bold;">CharIndex </span>and secondly, the difficulty of creating patterns for <span style="font-weight:bold;">PatIndex </span>to search. Developers try to solve some typical problems faced by today&#8217;s web application by developing their own workarounds instead of using <span style="font-weight:bold;">PatIndex</span>.<br />Today, I will share some very useful tricks which can help you to make sure that your input string is according to the pattern you want like all numeric, all letters, starts with letter and end with number etc.</p>
<p>Following are the TSQL scripts which you can use to solve these type of problems. You can convert them to user defined functions as per your requirements:<br /> 
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8212;Checks that input only contains numbers</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:blue;">if</span><span style="font-size:10pt;font-family:&quot;"><span>  </span><span style="color:fuchsia;">PatIndex</span><span style="color:gray;">(</span><span style="color:red;">&#8216;%[^0-9]%&#8217;</span><span style="color:gray;">,</span><span style="color:red;">&#8216;11&#8242;</span><span style="color:gray;">)</span> <span style="color:gray;">&gt;</span> 0</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Not all numbers&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br />Else<br />Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;All numbers&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8212;Checks that input only contains letters</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">if</span><span style="font-size:10pt;font-family:&quot;"><span>  </span><span style="color:fuchsia;">PatIndex</span><span style="color:gray;">(</span><span style="color:red;">&#8216;%[^a-z]%&#8217;</span><span style="color:gray;">,</span><span style="color:red;">&#8216;aaaaa&#8217;</span><span style="color:gray;">)</span> <span style="color:gray;">&gt;</span> 0</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Not all letters&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br />Else<br />Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;All letters&#8217;<br /></span></span><span style="font-size:10pt;font-family:&quot;color:blue;">End<br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8211;Checking for mixed input</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">If</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:fuchsia;">PatIndex</span><span style="color:gray;">(</span><span style="color:red;">&#8216;%[^0-9][0-9]%&#8217;</span><span style="color:gray;">,</span><span style="color:red;">&#8216;abc&#8217;</span><span style="color:gray;">)</span> <span style="color:gray;">&gt;</span> 0</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Alpha numeric data&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br />Else<br />Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Either all numbers or all letters&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8211;Checks that value must start with a letter and a number</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">If</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:fuchsia;">PatIndex</span><span style="color:gray;">(</span><span style="color:red;">&#8216;[^0-9][0-9]%&#8217;</span><span style="color:gray;">,</span><span style="color:red;">&#8216;A1anamwar11&#8242;</span><span style="color:gray;">)</span> <span style="color:gray;">&gt;</span> 0</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Starts with a letter and a number&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br />Else<br />Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Does not start with a letter and a number&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8211;Checks that value must End with a letter and a number</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">If</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:fuchsia;">PatIndex</span><span style="color:gray;">(</span><span style="color:red;">&#8216;%[^0-9][0-9]&#8216;</span><span style="color:gray;">,</span><span style="color:red;">&#8216;A1anamwar11a1&#8242;</span><span style="color:gray;">)</span> <span style="color:gray;">&gt;</span> 0</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Ends with a letter and a number&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br />Else<br />Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Does not End with a letter and a number&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8211;Checks that value must Start with a letter and Ends with a number</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">If</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:fuchsia;">PatIndex</span><span style="color:gray;">(</span><span style="color:red;">&#8216;[^0-9]%[0-9]&#8216;</span><span style="color:gray;">,</span><span style="color:red;">&#8216;namwar1&#8242;</span><span style="color:gray;">)</span> <span style="color:gray;">&gt;</span> 0</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Starts with a letter and ends with a number&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br />Else<br />Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Does not start with a letter and ends with a number&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End</span></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqltips.wordpress.com/92/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqltips.wordpress.com/92/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltips.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltips.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltips.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltips.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltips.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltips.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltips.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltips.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltips.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltips.wordpress.com/92/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=92&subd=sqltips&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sqltips.wordpress.com/2007/09/05/validating-inputs-and-finding-patterns-with-patindex/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f594e19b68c1c1098c99fb65c67717f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">namwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Conditionally add a column in the table</title>
		<link>http://sqltips.wordpress.com/2007/09/03/conditionally-add-a-column-in-the-table/</link>
		<comments>http://sqltips.wordpress.com/2007/09/03/conditionally-add-a-column-in-the-table/#comments</comments>
		<pubDate>Mon, 03 Sep 2007 19:24:00 +0000</pubDate>
		<dc:creator>namwar</dc:creator>
				<category><![CDATA[DMV]]></category>
		<category><![CDATA[Information]]></category>
		<category><![CDATA[New Features]]></category>
		<category><![CDATA[Object Search]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[Search columns]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://sqltips.wordpress.com/2007/09/03/conditionally-add-a-column-in-the-table/</guid>
		<description><![CDATA[Sometimes we need to add a column in an already existing table but we need to make sure that this column should not be present already.  This conditional column creation requires a way to check column existence in the given table. SQL Server 2005 INFORMATION_SCHEMA.COLUMNS view  is an standard way to do this. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=91&subd=sqltips&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometimes we need to add a column in an already existing table but we need to make sure that this column should not be present already.  This conditional column creation requires a way to check column existence in the given table. SQL Server 2005 <span style="font-weight:bold;">INFORMATION_SCHEMA.COLUMNS</span> view  is an standard way to do this. Following script gives you the ability to conditionally add a column in the table, if that column is not already present.</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:blue;">Use</span><span style="font-size:10pt;font-family:&quot;"> tempdb<br />Go<br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:blue;">Create</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">table</span> testTable <span style="color:gray;">(</span>id <span style="color:blue;">int</span><span style="color:gray;">,</span> city <span style="color:blue;">varchar</span><span style="color:gray;">(</span>50<span style="color:gray;">))</span><br />Go<br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8212;Check for column existence</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">If</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:gray;">not</span> <span style="color:gray;">exists</span></span><br /><span style="font-size:10pt;font-family:&quot;color:gray;">(</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:blue;">Select</span> <span style="color:gray;">*</span> <span style="color:blue;">from</span> <span style="color:green;">INFORMATION_SCHEMA.COLUMNS</span><br /><span>      </span><span style="color:blue;">Where</span><br /><span>            </span>TABLE_CATALOG<span style="color:gray;">=</span><span style="color:red;">&#8216;tempdb&#8217;</span><br /><span>      </span><span style="color:gray;">AND</span><span>   </span>TABLE_SCHEMA<span style="color:gray;">=</span><span style="color:red;">&#8216;dbo&#8217;</span><br /><span>      </span><span style="color:gray;">AND</span><span>   </span>TABLE_NAME<span style="color:gray;">=</span><span style="color:red;">&#8216;testTable&#8217;</span><br /><span>      </span><span style="color:gray;">AND</span><span>   </span>COLUMN_NAME<span style="color:gray;">=</span><span style="color:red;">&#8216;id&#8217;<br /></span></span><span style="font-size:10pt;font-family:&quot;color:gray;">)</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:green;">&#8211;Add new column</span><br /><span>      </span><span style="color:blue;">ALTER</span> <span style="color:blue;">Table</span> dbo<span style="color:gray;">.</span>testTable <span style="color:blue;">Add</span> id <span style="color:blue;">int</span> <span style="color:gray;">Null</span><br /><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Column added successfuly&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End<br />Else<br />Begin</span><br /><span style="font-size:10pt;font-family:&quot;"><span>      </span><span style="color:green;">&#8211;Column already exists. leave the table as it is.</span><br /><span>      </span><span style="color:blue;">Print</span> <span style="color:red;">&#8216;Column already exists.&#8217;</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">End</span><br /><span style="font-size:10pt;font-family:&quot;">Go<br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:blue;">Drop</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">table</span> testTable<br />Go</span></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqltips.wordpress.com/91/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqltips.wordpress.com/91/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltips.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltips.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltips.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltips.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltips.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltips.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltips.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltips.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltips.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltips.wordpress.com/91/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=91&subd=sqltips&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sqltips.wordpress.com/2007/09/03/conditionally-add-a-column-in-the-table/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f594e19b68c1c1098c99fb65c67717f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">namwar</media:title>
		</media:content>
	</item>
		<item>
		<title>Number padding in TSQL</title>
		<link>http://sqltips.wordpress.com/2007/08/24/number-padding-in-tsql/</link>
		<comments>http://sqltips.wordpress.com/2007/08/24/number-padding-in-tsql/#comments</comments>
		<pubDate>Fri, 24 Aug 2007 21:24:00 +0000</pubDate>
		<dc:creator>namwar</dc:creator>
				<category><![CDATA[TSQL]]></category>
		<category><![CDATA[string concatenation]]></category>
		<category><![CDATA[string manipulation]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://sqltips.wordpress.com/2007/08/24/number-padding-in-tsql/</guid>
		<description><![CDATA[Sometime we need to pad leading zeros to numeric values so that they can appear having same widthlike 00001,0022,0934 etc. Following code demonstrates the technique to pad leading zeros in front of digits.
&#8211;Variable to hold max lengthDeclare @m_maxLength int
&#8211;Create a test tableDeclare @m_testTable table (sampleValue int)
&#8211;Insert some sample valuesInsert into @m_testTable values (1)Insert into @m_testTable [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=84&subd=sqltips&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometime we need to pad leading zeros to numeric<br /> values so that they can appear having same width<br />like 00001,0022,0934 etc. <br />Following code demonstrates the technique<br /> to pad leading zeros in front of digits.</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8211;Variable to hold max length</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Declare</span><span style="font-size:10pt;font-family:&quot;"> @m_maxLength <span style="color:blue;">int</span></span><span style="font-size:10pt;font-family:&quot;color:blue;"><br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8211;Create a test table</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Declare</span><span style="font-size:10pt;font-family:&quot;"> @m_testTable <span style="color:blue;">table</span> <span style="color:gray;">(</span>sampleValue <span style="color:blue;">int</span><span style="color:gray;">)</span></span><span style="font-size:10pt;font-family:&quot;color:gray;"><br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8211;Insert some sample values</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> @m_testTable <span style="color:blue;">values</span> <span style="color:gray;">(</span>1<span style="color:gray;">)</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> @m_testTable <span style="color:blue;">values</span> <span style="color:gray;">(</span>2<span style="color:gray;">)</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> @m_testTable <span style="color:blue;">values</span> <span style="color:gray;">(</span>19<span style="color:gray;">)</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> @m_testTable <span style="color:blue;">values</span> <span style="color:gray;">(</span>201<span style="color:gray;">)</span></span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Insert</span><span style="font-size:10pt;font-family:&quot;"> <span style="color:blue;">into</span> @m_testTable <span style="color:blue;">values</span> <span style="color:gray;">(</span>20231<span style="color:gray;">)</span></span><span style="font-size:10pt;font-family:&quot;color:gray;"><br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8211;Calcualte the length of maximum number in the table</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Select</span><span style="font-size:10pt;font-family:&quot;"><br />@m_maxLength<span style="color:gray;">=</span><span style="color:fuchsia;">Max</span><span style="color:gray;">(</span><span style="color:fuchsia;">Len</span><span style="color:gray;">(</span><span style="color:fuchsia;">convert</span><span style="color:gray;">(</span><span style="color:blue;">varchar</span><span style="color:gray;">,</span>sampleValue<span style="color:gray;">)))<br /></span><span style="color:blue;">from</span> @m_testTable<br /></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:green;">&#8212;Now select the values</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">Select<br /></span><span style="font-size:10pt;font-family:&quot;">sampleValue<span style="color:gray;">,</span><br /><span style="color:fuchsia;">Replicate</span><span style="color:gray;">(</span><span style="color:red;">&#8216;0&#8242;</span><span style="color:gray;">,</span>@m_maxLength<span style="color:gray;">-</span><span style="color:fuchsia;">Len</span><span style="color:gray;">(</span><span style="color:fuchsia;">convert</span><span style="color:gray;">(</span><span style="color:blue;">varchar</span><span style="color:gray;">,</span>sampleValue<span style="color:gray;">)))<br />+</span><span style="color:fuchsia;">convert</span><span style="color:gray;">(</span><span style="color:blue;">varchar</span><span style="color:gray;">,</span>sampleValue<span style="color:gray;">)</span> <span style="color:blue;">as</span> paddedValue</span><br /><span style="font-size:10pt;font-family:&quot;color:blue;">from</span><span style="font-size:10pt;font-family:&quot;"> @m_testTable</span></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqltips.wordpress.com/84/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqltips.wordpress.com/84/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltips.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltips.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltips.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltips.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltips.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltips.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltips.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltips.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltips.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltips.wordpress.com/84/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=84&subd=sqltips&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sqltips.wordpress.com/2007/08/24/number-padding-in-tsql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f594e19b68c1c1098c99fb65c67717f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">namwar</media:title>
		</media:content>
	</item>
		<item>
		<title>ENABLE/DISABLE all triggers of all tables in one statement</title>
		<link>http://sqltips.wordpress.com/2007/08/20/enabledisable-all-triggers-of-all-tables-in-one-statement/</link>
		<comments>http://sqltips.wordpress.com/2007/08/20/enabledisable-all-triggers-of-all-tables-in-one-statement/#comments</comments>
		<pubDate>Mon, 20 Aug 2007 20:15:00 +0000</pubDate>
		<dc:creator>namwar</dc:creator>
				<category><![CDATA[Shortcuts]]></category>
		<category><![CDATA[Undocumented features]]></category>
		<category><![CDATA[Utility Functions]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://sqltips.wordpress.com/2007/08/20/enabledisable-all-triggers-of-all-tables-in-one-statement/</guid>
		<description><![CDATA[Sometimes we need to disable triggers to perform some tasks. Disabling the trigger by going to each table is very tedious. Following is avery quick way of disabling all triggers on all tables of the given database in a single statement.
sp_msforeachtable &#8220;ALTER TABLE ? DISABLE TRIGGER  all&#8221;
To enable all triggers, you can use following [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=81&subd=sqltips&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sometimes we need to disable triggers to perform some tasks. Disabling the trigger by going to each table is very tedious. Following is avery quick way of disabling all triggers on all tables of the given database in a single statement.</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:maroon;">sp_msforeachtable</span><span style="font-size:10pt;font-family:&quot;"> &#8220;ALTER TABLE ? DISABLE TRIGGER<span>  </span>all&#8221;</span></p>
<p>To enable all triggers, you can use following statement
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;color:maroon;">sp_msforeachtable</span><span style="font-size:10pt;font-family:&quot;"> &#8220;ALTER TABLE ? ENABLE TRIGGER<span>  </span>all&#8221;</span></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqltips.wordpress.com/81/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqltips.wordpress.com/81/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqltips.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqltips.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqltips.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqltips.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqltips.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqltips.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqltips.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqltips.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqltips.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqltips.wordpress.com/81/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqltips.wordpress.com&blog=1606781&post=81&subd=sqltips&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://sqltips.wordpress.com/2007/08/20/enabledisable-all-triggers-of-all-tables-in-one-statement/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f594e19b68c1c1098c99fb65c67717f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">namwar</media:title>
		</media:content>
	</item>
	</channel>
</rss>