Home Ask Login Register

Developers Planet

Your answer is one click away!

boot4life February 2016

What's a good way to trim all whitespace characters from a string in T-SQL without UDF and without CLR?

The .NET function string.Trim trims a rather extensive set of whitespace characters. How would this exact behavior be emulated in the best way T-SQL?

LTRIM and RTRIM only trim the space character which is not enough.

This would be easy with SQL CLR but that is not always possible and also has quite a high invocation cost. So I would like to avoid SQL CLR.

Also, scalar UDFs force serial plans and are slow to invoke. So it probably should not be a scalar UDF, either.

Given these constraints what's the best way to go about this?

Answers


JJ32 February 2016

I'll be interested to see if anyone finds a generic SQL solution.

The best I can come up with is a simple REPLACE function:

SELECT MyString = LEFT(MyString, LEN(RTRIM(REPLACE(REPLACE(REPLACE(MyString COLLATE Latin1_General_100_BIN2, NCHAR(9), ' '), NCHAR(12), ' '), NCHAR(13), ' ')))) AS RTrimmed

SELECT MyString = RIGHT(MyString, LEN(LTRIM(REPLACE(REPLACE(REPLACE(MyString COLLATE Latin1_General_100_BIN2, NCHAR(9), ' '), NCHAR(12), ' '), NCHAR(13), ' ')))) AS LTrimmed

etc.

You can get the list of current whitespace characters here:

http://unicode.org/charts/uca/chart_Whitespace.html

Or, to prove it to yourself, you could export a list of all characters from SQL Server to something like Excel, clean the characters, and import them back in. Whatever was removed was whitespace.


HABO February 2016

This code provides a pattern that you can extend to handle the whitespace of your choice for a modified LTRIM.

declare @Tab as NVarChar(1) = NChar( 9 );
declare @Space as NVarChar(1) = NChar( 32 );

declare @Samples as Table ( String NVarChar(16) );
insert into @Samples ( String ) values
  ( 'Foo' ),
  ( @Tab + 'Foo' ),
  ( @Space + 'Foo' ),
  ( @Space + @Tab + 'Foo' ),
  ( @Tab + @Space + 'Foo' );
select String, Len( String ) as [Length], PatIndex( '%[^' + @Tab + @Space + ']%', String ) - 1 as [WhitespaceCount]
  from @Samples;

The REVERSE function can be used to implement a modified version of RTRIM.

NEWER UPDATE: The following code uses the list of whitespace characters as used in .NET Framework 4. It also works around the feature of LEN not counting trailing blanks.

declare @Tab as NVarChar(1) = NChar( 9 );
declare @Space as NVarChar(1) = NChar( 32 );

declare @Samples as Table ( String NVarChar(16) );
insert into @Samples ( String ) values
  ( 'Foo' ),
  ( @Tab + 'Foo' ),
  ( @Space + 'Foo' ),
  ( @Space + @Tab + 'Foo' ),
  ( @Tab + @Space + 'Foo' ),
  ( @Tab + 'Foo' + @Space ),
  ( @Space + 'Foo' + @Tab ),
  ( @Space + @Tab + 'Foo' + @Tab + @Space ),
  ( @Tab + @Space + 'Foo' + @Space + @Tab ),
  ( 'Foo' + @Tab ),
  ( NULL ),
  ( '           ' ),
  ( @Space + NULL + @Tab + @Tab ),
  ( '' ),
  ( 'Hello world!' );

declare @WhitespacePattern as NVarChar(100) = N'%[^' +
  NChar( 0x0020 ) + NChar( 0x00A0 ) + NChar( 0x1680 ) + NChar( 0x2000 ) +
  NChar( 0x2001 ) + NChar( 0x2002 ) + NChar( 0x2003 ) + NChar( 0x2004 ) +
  NChar( 0x2005 ) + NChar( 0x2006 ) + NChar( 0x2007 ) + NChar( 0x2008 ) +
  NChar( 0x2009 ) + NChar( 0x200A ) + NChar( 0x202F ) + NChar( 0x205F ) +
  NChar( 0x3000 ) + NChar( 0x2028 ) + NChar( 0x2029 ) 

Post Status

Asked in February 2016
Viewed 2,347 times
Voted 11
Answered 2 times

Search




Leave an answer


Quote of the day: live life