<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	>
<channel>
	<title>Comments on: SQL SERVER - 2008 - Enhenced TRIM() Function - Remove Trailing Spaces, Leading Spaces, White Space, Tabs, Carriage Returns, Line Feeds</title>
	<atom:link href="http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<pubDate>Wed, 07 Jan 2009 16:50:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: LOL@Enhenced</title>
		<link>http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/#comment-45018</link>
		<dc:creator>LOL@Enhenced</dc:creator>
		<pubDate>Mon, 22 Dec 2008 22:36:13 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1346#comment-45018</guid>
		<description>I'm going with phil's function. It doesn't do any more or less than what I want.</description>
		<content:encoded><![CDATA[<p>I&#8217;m going with phil&#8217;s function. It doesn&#8217;t do any more or less than what I want.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Speednet</title>
		<link>http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/#comment-43792</link>
		<dc:creator>Speednet</dc:creator>
		<pubDate>Sat, 18 Oct 2008 15:09:05 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1346#comment-43792</guid>
		<description>@phil Factor: If you try my version of SuperTrim, you will see that it does NOT trim to 8000 characters.</description>
		<content:encoded><![CDATA[<p>@phil Factor: If you try my version of SuperTrim, you will see that it does NOT trim to 8000 characters.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nathan</title>
		<link>http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/#comment-43778</link>
		<dc:creator>Nathan</dc:creator>
		<pubDate>Fri, 17 Oct 2008 16:19:59 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1346#comment-43778</guid>
		<description>Pinal, thanks for all your posts.  May I suggest using the spelling "Enhanced" instead of "Enhenced" in your post?

Thanks again!</description>
		<content:encoded><![CDATA[<p>Pinal, thanks for all your posts.  May I suggest using the spelling &#8220;Enhanced&#8221; instead of &#8220;Enhenced&#8221; in your post?</p>
<p>Thanks again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: phil Factor</title>
		<link>http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/#comment-43661</link>
		<dc:creator>phil Factor</dc:creator>
		<pubDate>Sun, 12 Oct 2008 10:41:41 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1346#comment-43661</guid>
		<description>The function definitely just returns the first 8000 characters. This version definitely works a lot better

alter FUNCTION dbo.LTrimX(@str VARCHAR(MAX)) RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @trimchars VARCHAR(10)
SET @trimchars = CHAR(9)+CHAR(10)+CHAR(13)+CHAR(32)
IF @str LIKE '[' + @trimchars + ']%' 
	SET @str =STUFF ( @str , 1 , PATINDEX('%[^' + @trimchars + ']%', @str)-1 ,'' )
RETURN @str
END
GO</description>
		<content:encoded><![CDATA[<p>The function definitely just returns the first 8000 characters. This version definitely works a lot better</p>
<p>alter FUNCTION dbo.LTrimX(@str VARCHAR(MAX)) RETURNS VARCHAR(MAX)<br />
AS<br />
BEGIN<br />
DECLARE @trimchars VARCHAR(10)<br />
SET @trimchars = CHAR(9)+CHAR(10)+CHAR(13)+CHAR(32)<br />
IF @str LIKE &#8216;[' + @trimchars + ']%&#8217;<br />
	SET @str =STUFF ( @str , 1 , PATINDEX(&#8217;%[^' + @trimchars + ']%&#8217;, @str)-1 ,&#8221; )<br />
RETURN @str<br />
END<br />
GO</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil Factor</title>
		<link>http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/#comment-43660</link>
		<dc:creator>Phil Factor</dc:creator>
		<pubDate>Sun, 12 Oct 2008 10:14:04 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1346#comment-43660</guid>
		<description>Oh dear, surely, unless I'm missing something  these functions will just return the first 8000 characters if they do a Trim, and so could cause havoc if someone uses it in SQL Server 2005/8 without realising its limitations, or you have it in place in a production system and the data changes. You'd do better if it took and returned Varchar(8000). Then it might flag up the weakness in the algorithm.</description>
		<content:encoded><![CDATA[<p>Oh dear, surely, unless I&#8217;m missing something  these functions will just return the first 8000 characters if they do a Trim, and so could cause havoc if someone uses it in SQL Server 2005/8 without realising its limitations, or you have it in place in a production system and the data changes. You&#8217;d do better if it took and returned Varchar(8000). Then it might flag up the weakness in the algorithm.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Speednet</title>
		<link>http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/#comment-43652</link>
		<dc:creator>Speednet</dc:creator>
		<pubDate>Sat, 11 Oct 2008 04:43:24 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1346#comment-43652</guid>
		<description>Hi Pinal,

May I suggest some improvements?

I tend to call my whitespace trimmers "SuperTrim" functions, so please excuse the difference in naming.

(This will probably lose its indenting when I paste it.)

---------
CREATE FUNCTION dbo.SuperTrimLeft(@str varchar(MAX)) RETURNS varchar(MAX)
AS
BEGIN
	IF (ASCII(LEFT(@str, 1)) &#60; 33) BEGIN
		SET @str = STUFF(@str, 1, PATINDEX('%[^'+CHAR(0)+'-'+CHAR(32)+']%', @str) - 1, '');
	END;

	RETURN @str;
END;
---------

I think it's a big performance increase, because it does a numeric test of the first character, rather than a relatively expensive pattern test.  If the first character is not a space or some other control character, no pattern is tested at all.

Then, instead of testing for specific control characters, why not test for the whole range of zero (0) through 32 (space)?

The function above does the test for first non-whitespace char and the trimming all in one statement.

I couldn't think of a faster or more efficient way to do it.  I did some testing, and it seemed to work well.

I haven't looked at different ways to do SuperTrimRight yet, but the ways you outlined should work with this function OK.

Thanks for the great SQL blog!

-Todd ("Speednet")</description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>May I suggest some improvements?</p>
<p>I tend to call my whitespace trimmers &#8220;SuperTrim&#8221; functions, so please excuse the difference in naming.</p>
<p>(This will probably lose its indenting when I paste it.)</p>
<p>&#8212;&#8212;&#8212;<br />
CREATE FUNCTION dbo.SuperTrimLeft(@str varchar(MAX)) RETURNS varchar(MAX)<br />
AS<br />
BEGIN<br />
	IF (ASCII(LEFT(@str, 1)) &lt; 33) BEGIN<br />
		SET @str = STUFF(@str, 1, PATINDEX(&#8217;%[^'+CHAR(0)+'-'+CHAR(32)+']%&#8217;, @str) - 1, &#8221;);<br />
	END;</p>
<p>	RETURN @str;<br />
END;<br />
&#8212;&#8212;&#8212;</p>
<p>I think it&#8217;s a big performance increase, because it does a numeric test of the first character, rather than a relatively expensive pattern test.  If the first character is not a space or some other control character, no pattern is tested at all.</p>
<p>Then, instead of testing for specific control characters, why not test for the whole range of zero (0) through 32 (space)?</p>
<p>The function above does the test for first non-whitespace char and the trimming all in one statement.</p>
<p>I couldn&#8217;t think of a faster or more efficient way to do it.  I did some testing, and it seemed to work well.</p>
<p>I haven&#8217;t looked at different ways to do SuperTrimRight yet, but the ways you outlined should work with this function OK.</p>
<p>Thanks for the great SQL blog!</p>
<p>-Todd (&#8221;Speednet&#8221;)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
