<?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>code.commongroove.com &#187; MS SQL Server</title>
	<atom:link href="http://code.commongroove.com/category/ms-sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.commongroove.com</link>
	<description>C#, T-SQL, and general IT mojo</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:00:48 +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>SSIS: Use Two OLE DB Destination Adapters to Catch Insertion/Constraint Errors</title>
		<link>http://code.commongroove.com/2011/09/16/ssis-use-two-ole-db-destination-adapters-to-catch-insertionconstraint-errors/</link>
		<comments>http://code.commongroove.com/2011/09/16/ssis-use-two-ole-db-destination-adapters-to-catch-insertionconstraint-errors/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 15:27:27 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/?p=307</guid>
		<description><![CDATA[If you are using SQL Server Integration Server (SSIS) packages to insert large volumes of data, you have undoubtedly encountered incorrectly formatted or typed data in your data source including precision errors, conversion errors, and primary key/foreign key constraint errors etc. To &#8230; <a href="http://code.commongroove.com/2011/09/16/ssis-use-two-ole-db-destination-adapters-to-catch-insertionconstraint-errors/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you are using SQL Server Integration Server (SSIS) packages to insert large volumes of data, you have undoubtedly encountered incorrectly formatted or typed data in your data source including precision errors, conversion errors, and primary key/foreign key constraint errors etc.</p>
<p>To avoid package failure, you can choose to redirect error output to an alternate destination rather.  However, if you are using &#8220;Table or view &#8211; fast load&#8221; as your insertion method, it can be difficult to see which rows are actually the offenders in your redirected output.</p>
<p>One way to get around this is to <strong>use two OLE DB Destination adapters with different table access modes and then redirect the output</strong>.</p>
<p>On the first OLD DB Destination adapter, choose &#8220;Table or view &#8211; fast load&#8221; as the access mode, but set the maximum commit size to a specific number appropriate for your data set size.  Remember the smaller the commit size the slower the package will run.  In my particular case, I found a commit size of 5000 was appropriate for the data source.</p>
<p>Next, connect the error output from your first OLE DB Destination adapter to a second OLE DB Destination adapter rather than directly to a &#8220;bad input&#8221; file or database destination.  On this adapter, choose &#8220;Table or view&#8221; as the access mode which will insert records into the database one-by-one (another reason why you will have to tune your first adapter&#8217;s commit size to get optimal balance between performance and error handling).</p>
<p>Finally, take the error output from this second OLE DB Destination adapter and connect it to your &#8220;bad input&#8221; destination.</p>
<p>When it runs, the data batch that has the bad data on the first adapter will get redirected to the second adapter rather than having the whole commit size set being dumped to your error output file.  And because it is inserting one-by-one with &#8220;Table or view&#8221; access, you will only get the &#8220;real&#8221; errors dumped out to your error destination.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2011/09/16/ssis-use-two-ole-db-destination-adapters-to-catch-insertionconstraint-errors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MS SQL Server: Quick T-SQL to Delete All User Table Data</title>
		<link>http://code.commongroove.com/2011/03/29/ms-sql-server-quick-t-sql-to-delete-all-user-table-data/</link>
		<comments>http://code.commongroove.com/2011/03/29/ms-sql-server-quick-t-sql-to-delete-all-user-table-data/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 15:28:20 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/?p=266</guid>
		<description><![CDATA[Here&#8217;s a quick (hack) way to delete all user table data from a Microsoft SQL Server database using the undocumented procedure sp_MSforeachtable. As the name hints, this procedure repeats a SQL statement against all user tables within a database. This &#8230; <a href="http://code.commongroove.com/2011/03/29/ms-sql-server-quick-t-sql-to-delete-all-user-table-data/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick (hack) way to delete all user table data from a Microsoft SQL Server database using the undocumented procedure <em>sp_MSforeachtable</em>. As the name hints, this procedure repeats a SQL statement against all user tables within a database.</p>
<p>This example assumes all identity columns are seeded at 1. You may not be able to do that in your environment.</p>
<p>Also note, you could use TRUNCATE TABLE, but only if you no foreign key constraints on your tables.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'<br />
EXEC sp_MSforeachtable 'DELETE ?'<br />
EXEC sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'<br />
EXEC sp_MSforeachtable 'IF (OBJECTPROPERTY(object_id(''?''), ''TableHasIdentity'') = 1) DBCC CHECKIDENT(''?'',RESEED,1)'</div></div>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2011/03/29/ms-sql-server-quick-t-sql-to-delete-all-user-table-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a SQL CLR User Defined Function</title>
		<link>http://code.commongroove.com/2011/03/07/creating-a-sql-clr-user-defined-function/</link>
		<comments>http://code.commongroove.com/2011/03/07/creating-a-sql-clr-user-defined-function/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 20:28:07 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/?p=254</guid>
		<description><![CDATA[Starting with SQL Server 2005, Microsoft added the awesome ability to reference .Net assemblies from your T-SQL procedures.  Here&#8217;s a quick 5 step overview on how to get up and running with your code-based User Defined Function. 1) Create a &#8230; <a href="http://code.commongroove.com/2011/03/07/creating-a-sql-clr-user-defined-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Starting with SQL Server 2005, Microsoft added the awesome ability to reference .Net assemblies from your T-SQL procedures.  Here&#8217;s a quick 5 step overview on how to get up and running with your code-based User Defined Function.</p>
<p>1) Create a project from the Visual C# SQL CLR Database Project template (just so you can get the correct references and get a test harness going).</p>
<p>2) Add a class containing your UDFs.  This one does a basic Regular Expression comparison:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MyUdf<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #008000;">&#91;</span>Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">SqlServer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Server</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SqlFunction</span><span style="color: #008000;">&#40;</span>IsDeterministic <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> SqlBoolean FnRegExMatch<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> source, <span style="color: #6666cc; font-weight: bold;">string</span> pattern<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
Match m <span style="color: #008000;">=</span> Regex<span style="color: #008000;">.</span><span style="color: #0000FF;">Match</span><span style="color: #008000;">&#40;</span>source, pattern,<br />
RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">CultureInvariant</span> <span style="color: #008000;">|</span><br />
RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreCase</span> <span style="color: #008000;">|</span><br />
RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Singleline</span><br />
<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">return</span> m<span style="color: #008000;">.</span><span style="color: #0000FF;">Success</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>3) Enable the CLR on your SQL Server instance through the configuration options:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;">EXEC</span> sp_configure <span style="color: #ff0000;">'show advanced options'</span> <span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'1'</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
RECONFIGURE<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span> sp_configure <span style="color: #ff0000;">'clr enabled'</span> <span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'1'</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
RECONFIGURE<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span> sp_configure <span style="color: #ff0000;">'show advanced options'</span> <span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'0'</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>4) Add the assembly and then create a reference to your UDF:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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> ASSEMBLY <span style="color: #66cc66;">&#91;</span>MyUdfs<span style="color: #66cc66;">&#93;</span><br />
AUTHORIZATION <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'C:<span style="color: #000099; font-weight: bold;">\S</span>qlServerClr<span style="color: #000099; font-weight: bold;">\M</span>yUdf.dll'</span><br />
<span style="color: #993333; font-weight: bold;">WITH</span> PERMISSION_SET <span style="color: #66cc66;">=</span> SAFE <span style="color: #808080; font-style: italic;">-- we only have &quot;safe&quot; calls in our lib</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>FnRegExMatch<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span><br />
@<span style="color: #993333; font-weight: bold;">SOURCE</span> nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
@pattern nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">RETURNS</span> BIT<br />
<span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #993333; font-weight: bold;">EXECUTE</span> <span style="color: #993333; font-weight: bold;">AS</span> CALLER<br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
EXTERNAL NAME <span style="color: #66cc66;">&#91;</span>MyUdf<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>FnRegExMatch<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>5) Now you should be able to call your function:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;">SELECT</span> dbo<span style="color: #66cc66;">.</span>FnRegExMatch<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'my test string'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\b</span>test<span style="color: #000099; font-weight: bold;">\b</span>'</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>Of course this is just the barest of essentials to get you going with SQL CLR UDFs, but I hope it helps!</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2011/03/07/creating-a-sql-clr-user-defined-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL: Backup All User Databases</title>
		<link>http://code.commongroove.com/2010/08/18/t-sql-backup-all-user-databases/</link>
		<comments>http://code.commongroove.com/2010/08/18/t-sql-backup-all-user-databases/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 14:35:07 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/?p=189</guid>
		<description><![CDATA[Here&#8217;s an easy, generic way to back up all user databases on a SQL Server instance: DECLARE @name VARCHAR&#40;50&#41; -- database name DECLARE @path VARCHAR&#40;256&#41; -- path for backup files DECLARE @fileName VARCHAR&#40;256&#41; -- filename for backup DECLARE @fileDate VARCHAR&#40;20&#41; -- &#8230; <a href="http://code.commongroove.com/2010/08/18/t-sql-backup-all-user-databases/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s an easy, generic way to back up all user databases on a SQL Server instance:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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> @name <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- database name</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @path <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">256</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- path for backup files</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @fileName <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">256</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- filename for backup</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @fileDate <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- used for file name</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> @path <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'D:<span style="color: #000099; font-weight: bold;">\l</span>ocalBackups<span style="color: #000099; font-weight: bold;">\'</span> -- has to be a local path<br />
<br />
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)<br />
<br />
DECLARE db_cursor CURSOR FOR<br />
SELECT name<br />
FROM master.dbo.sysdatabases<br />
WHERE name NOT IN ('</span>master<span style="color: #ff0000;">','</span>model<span style="color: #ff0000;">','</span>msdb<span style="color: #ff0000;">','</span>tempdb<span style="color: #ff0000;">')<br />
<br />
OPEN db_cursor<br />
FETCH NEXT FROM db_cursor INTO @name<br />
<br />
WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
SET @fileName = @path + @name + '</span>_<span style="color: #ff0000;">' + @fileDate + '</span><span style="color: #66cc66;">.</span>BAK<span style="color: #ff0000;">'<br />
BACKUP DATABASE @name TO DISK = @fileName<br />
<br />
FETCH NEXT FROM db_cursor INTO @name<br />
END<br />
<br />
CLOSE db_cursor<br />
DEALLOCATE db_cursor</span></div></div>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2010/08/18/t-sql-backup-all-user-databases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shrinking your Transaction Log with SQL Server 2008 to Free Disk Space</title>
		<link>http://code.commongroove.com/2010/02/05/shrinking-your-transaction-log-with-sql-server-2008-to-free/</link>
		<comments>http://code.commongroove.com/2010/02/05/shrinking-your-transaction-log-with-sql-server-2008-to-free/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 00:02:53 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2010/02/04/shrinking-your-transaction-log-with-sql-server-2008-to-free.aspx</guid>
		<description><![CDATA[If you have been used to clearing up disk space on your development and test SQL server instance file systems with SQL Server 2000 or SQL Server 2005 by using the famous TRUNCATEONLY option on your transaction logs, you might be &#8230; <a href="http://code.commongroove.com/2010/02/05/shrinking-your-transaction-log-with-sql-server-2008-to-free/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you have been used to clearing up disk space on your development and test SQL server instance file systems with SQL Server 2000 or SQL Server 2005 by using the famous TRUNCATEONLY option on your transaction logs, you might be disheartened that they have removed this option from SQL Server 2008 (with good reason some might say).</p>
<p>If you do try to use it, you will get the infamous:</p>
<p><span style="font-family: Courier New; color: #ff0000;">&#8216;TRUNCATEONLY&#8217; is not a recognized BACKUP option.</span></p>
<p>To achieve the same effect with SQL Server 2008, you can toggle the recovery mode for the target database and then call your DBCC SHRINKFILE to clear up disk space hogged by your transaction log.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;">USE</span> MyDatabase<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> MyDatabase <span style="color: #993333; font-weight: bold;">SET</span> RECOVERY SIMPLE<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
DBCC SHRINKFILE<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'MyDatabase_log'</span><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;">GO</span><br />
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> MyDatabase <span style="color: #993333; font-weight: bold;">SET</span> RECOVERY <span style="color: #993333; font-weight: bold;">FULL</span><br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>(Of course if you are already using simple recovery, you wouldn&#8217;t need to toggle.)</p>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2010/02/05/shrinking-your-transaction-log-with-sql-server-2008-to-free/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t get caught by ANSI PADDING, VARCHAR, and trailing whitespace!</title>
		<link>http://code.commongroove.com/2010/01/28/dont-get-caught-by-ansi-padding-varchar-and-trailing-whitespace/</link>
		<comments>http://code.commongroove.com/2010/01/28/dont-get-caught-by-ansi-padding-varchar-and-trailing-whitespace/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 01:01:12 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2010/01/27/dont-get-caught-by-ansi-padding-varchar-and-trailing-whitespace.aspx</guid>
		<description><![CDATA[One thing to be aware of when you have VARCHAR columns and are using ANSI PADDING: trailing whitespace is trimmed and not counted in your equality ( = / &#60;&#62; / LIKE) statements, so you might not get the results you are looking to be &#8230; <a href="http://code.commongroove.com/2010/01/28/dont-get-caught-by-ansi-padding-varchar-and-trailing-whitespace/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One thing to be aware of when you have VARCHAR columns and are using ANSI PADDING: trailing whitespace is trimmed and not counted in your equality ( = / &lt;&gt; / LIKE) statements, so you might not get the results you are looking to be returned from your query.</p>
<p>In the same vein, the len() function will return the same value for two strings if one has trailing whitespace.  If you need to compare those two fields including trailing whitespace values, use datalength() instead.</p>
<p>Here&#8217;s some example T-SQL:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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> @<span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">FIELD</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</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;">TABLE</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'some text'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> @<span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'some text &nbsp; &nbsp; &nbsp; '</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> @<span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">' &nbsp; &nbsp;some text'</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns 2 :</span><br />
<span style="color: #808080; font-style: italic;">-- trailing spaces trimmed,</span><br />
<span style="color: #808080; font-style: italic;">-- leading spaces count</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'&quot;'</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">FIELD</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&quot;'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> @<span style="color: #993333; font-weight: bold;">TABLE</span><br />
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">FIELD</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'some text'</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- also returns 2</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'&quot;'</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">FIELD</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'&quot;'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> @<span style="color: #993333; font-weight: bold;">TABLE</span><br />
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">FIELD</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'some text'</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- shows the difference between len()</span><br />
<span style="color: #808080; font-style: italic;">-- and datalength()</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">FIELD</span><span style="color: #66cc66;">,</span> LEN<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">FIELD</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'Length'</span><span style="color: #66cc66;">,</span> DATALENGTH<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">FIELD</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'DataLength'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> @<span style="color: #993333; font-weight: bold;">TABLE</span></div></div>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2010/01/28/dont-get-caught-by-ansi-padding-varchar-and-trailing-whitespace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>December 2009 IT Toolbox for TechNet Magazine</title>
		<link>http://code.commongroove.com/2009/12/08/december-2009-it-toolbox-for-technet-magazine/</link>
		<comments>http://code.commongroove.com/2009/12/08/december-2009-it-toolbox-for-technet-magazine/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 20:36:19 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[Microsoft TechNet]]></category>
		<category><![CDATA[MS SQL Server]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2009/12/08/december-2009-it-toolbox-for-technet-magazine.aspx</guid>
		<description><![CDATA[The December 2009 TechNet Magazine is on the website. Check out my IT Toolbox column here: December 2009 TechNet Magazine IT Toolbox In this issue I covered: Eraser: Eradicate Sensitive Information SQL Server Backup Pro from Red Gate: Streamline SQL &#8230; <a href="http://code.commongroove.com/2009/12/08/december-2009-it-toolbox-for-technet-magazine/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The December 2009 <a title="TechNet Magazine" rel="" target="_blank" href="http://www.technetmagazine.com">TechNet Magazine</a> is on the website. Check out my IT Toolbox column here:</p>
<p><a target="_blank" href="http://technet.microsoft.com/en-us/magazine/ee835712.aspx">December 2009 TechNet Magazine IT Toolbox</a></p>
<p>In this issue I covered:</p>
<ul>
<li><strong>Eraser</strong>: Eradicate Sensitive Information</li>
<li><strong>SQL Server Backup Pro from Red Gate</strong>: Streamline SQL Server Backups</li>
<li><strong>MindManager</strong>: Map Out Ideas, Notes and Projects</li>
</ul>
<p>Check it out and let me know what you think!</p>
<p>And if you have a tool you want to see me review, please suggest it to me here: <a href="mailto:tntools@microsoft.com">tntools@microsoft.com</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2009/12/08/december-2009-it-toolbox-for-technet-magazine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL: Change Collation on your LIKE Clause Using COLLATE</title>
		<link>http://code.commongroove.com/2009/07/24/t-sql-change-collation-on-your-like-clause-using-collate/</link>
		<comments>http://code.commongroove.com/2009/07/24/t-sql-change-collation-on-your-like-clause-using-collate/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 22:17:25 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2009/07/24/t-sql-change-collation-on-your-like-clause-using-collate.aspx</guid>
		<description><![CDATA[Ever have trouble matching unicode letter equivalents in your T-SQL queries? For example, when you want &#8220;Québec&#8221; and &#8220;Quebec&#8221; to be equivalent in your text search query? This is where specifying Accent Insensative Collation (The &#8220;AI&#8221; in all those collation codes) comes &#8230; <a href="http://code.commongroove.com/2009/07/24/t-sql-change-collation-on-your-like-clause-using-collate/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Ever have trouble matching unicode letter equivalents in your T-SQL queries? For example, when <span>you want &#8220;Qu<strong>é</strong>bec&#8221; and &#8220;Qu<strong>e</strong>bec&#8221; to be equivalent in your text search query?</span></p>
<p>This is where specifying <strong>Accent Insensative Collation</strong> (The &#8220;AI&#8221; in all those collation codes) comes to the rescue.  If your database is already set to an AI collation, then you are all set already, but if not, change your LIKE statement to use something like the following:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;">SELECT</span> <span style="color: #66cc66;">*</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> MyTable <span style="color: #66cc66;">&#40;</span>NOLOCK<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">WHERE</span> MyColumn <span style="color: #993333; font-weight: bold;">COLLATE</span> SQL_Latin1_General_CP1_CI_AI <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%quebec%'</span></div></div>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2009/07/24/t-sql-change-collation-on-your-like-clause-using-collate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL Hack to Turn a Set of Row Values into a Delimited String</title>
		<link>http://code.commongroove.com/2009/02/26/t-sql-hack-to-turn-a-set-of-row-values-into/</link>
		<comments>http://code.commongroove.com/2009/02/26/t-sql-hack-to-turn-a-set-of-row-values-into/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 04:34:38 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2009/02/28/t-sql-hack-to-turn-a-set-of-row-values-into.aspx</guid>
		<description><![CDATA[Ever want to turn multiple rows into a comma separated list when you are querying your database? Here&#8217;s a quick way to get it done using &#8220;FOR XML PATH(&#8221;)&#8221;: DECLARE @comma_delimited_list VARCHAR&#40;6000&#41; SELECT @comma_delimited_list = &#40; SELECT productName + ',' &#8230; <a href="http://code.commongroove.com/2009/02/26/t-sql-hack-to-turn-a-set-of-row-values-into/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Ever want to turn multiple rows into a comma separated list when you are querying your database?</p>
<p>Here&#8217;s a quick way to get it done using &#8220;FOR XML PATH(&#8221;)&#8221;:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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> @comma_delimited_list <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6000</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @comma_delimited_list <span style="color: #66cc66;">=</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> productName <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">','</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> Products<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> productName <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'a%'</span><br />
<span style="color: #993333; font-weight: bold;">FOR</span> XML PATH<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">IF</span> LEN<span style="color: #66cc66;">&#40;</span>@comma_delimited_list<span style="color: #66cc66;">&#41;</span> &amp;gt; <span style="color: #cc66cc;">0</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @comma_delimited_list <span style="color: #66cc66;">=</span> STUFF<span style="color: #66cc66;">&#40;</span>@comma_delimited_list <span style="color: #66cc66;">,</span>LEN<span style="color: #66cc66;">&#40;</span>@comma_delimited_list<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>A couple things to remember: 1) you have set your path to &#8221; and 2) you can&#8217;t name your selection or it will put the name into your xml adding more information than you want.</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2009/02/26/t-sql-hack-to-turn-a-set-of-row-values-into/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Method of Finding Out What Version and Service Pack your SQL Server Instance is Running</title>
		<link>http://code.commongroove.com/2009/01/24/quick-method-of-finding-out-what-version-and-service-pack/</link>
		<comments>http://code.commongroove.com/2009/01/24/quick-method-of-finding-out-what-version-and-service-pack/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 00:36:43 +0000</pubDate>
		<dc:creator>Greg</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>

		<guid isPermaLink="false">http://code.commongroove.com/archive/2009/01/23/quick-method-of-finding-out-what-version-and-service-pack.aspx</guid>
		<description><![CDATA[If you are looking for a quick way to verify the service pack and version applied to your SQL server instance, try the following T-SQL: SELECT SERVERPROPERTY&#40;'productversion'&#41; AS productversion, SERVERPROPERTY&#40;'productlevel'&#41; AS productlevel, SERVERPROPERTY&#40;'edition'&#41; AS edition Hope this helps!]]></description>
			<content:encoded><![CDATA[<p>If you are looking for a quick way to verify the service pack and version applied to your SQL server instance, try the following T-SQL:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:550px;"><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;">SELECT</span><br />
SERVERPROPERTY<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'productversion'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> productversion<span style="color: #66cc66;">,</span><br />
SERVERPROPERTY<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'productlevel'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> productlevel<span style="color: #66cc66;">,</span><br />
SERVERPROPERTY<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'edition'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> edition</div></div>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://code.commongroove.com/2009/01/24/quick-method-of-finding-out-what-version-and-service-pack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

