Click here to Skip to main content
Click here to Skip to main content

LTRIM RTRIM doesn’t always work

, 18 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
LTRIM and RTRIM don't always remove all the whitespace characters in a field such as carriage return or linefeed
It is a frequent occurrence that we must remove leading and trailing whitespaces from a string before additional processing or sending it to another layer in an application. We can’t always control how the data is entered. The data might come from another system, a data conversion, an old application, EDI, Excel, or from an application which had poor quality control. In some of those cases, a whitespace might not be entered or saved in the system as character 32 which is a whitespace entered in a keyboard. If that happens, SQL built in functions for trimming whitespaces do not work so it becomes necessary to replace the “other” whitespace characters with character 32. Then LTRIM and RTRIM will work as expected.

I created this simple UDF to cleanup the data when necessary. I only use this when troubleshooting an old SQL 2000 application or pinpointing weird data coming into the Data Warehouse from the ERP.

Sample Usage

This example fixes all the product codes which have non printing white spaces. It runs the udf twice but you only touch the codes which need to be changed.
Update tblProduct
	set ProductCode = dbo.udfTrim(ProductCode)
where Len(ProductCode) != Len(dbo.udfTrim(ProductCode))

The UDF

/*
SQL 2000 Version
2/2/2012 CValenzuela
UDF to really trim the white spaces. 
When users copy and paste from Word, Excel, or some other application
into a text box, the special non printing whitespace characters 
like a line feed remain. This will replace all the non printing
whitespace characters with Character 32 which is the space bar then
perform an LTRIM and RTRIM
 
Declare 
@Seed as varchar(20),
@Test as varchar(50)
 
Set @Seed= ' CValenzuela';
Set @Test =  CHAR(0)+CHAR(9)+CHAR(10)+CHAR(11)+CHAR(12)+CHAR(13)+CHAR(14)+CHAR(160) + @Seed + CHAR(0)+CHAR(9)+CHAR(10)+CHAR(11)+CHAR(12)+CHAR(13)+CHAR(14)+CHAR(160)
 
Select
	@Seed as Seed,
	LTRIM(RTRIM(@SEED)) as Seed_Trimmed,	
	@Test as Test,
	LTRIM(RTRIM(@Test)) as Test_Trimmed,
	dbo.udfTrim(@Test) as Test_Trimmed2,
	
	Len(@Seed) as Seed_Length,
	DataLength(@Seed) as Seed_DataLength,
	LEN(LTRIM(RTRIM(@Seed))) as Seed_Trimmed_Length,
	DataLength(LTRIM(RTRIM(@Seed))) as Seed_Trimmed_DataLength,
 
	Len(@Test) as Test_Length,	
	LEN(LTRIM(RTRIM(@TEST))) as Test_Trimmed_Length,    	
	DataLength(LTRIM(RTRIM(@TEST))) as Test_Trimmed_DataLength,    	
	LEN(dbo.udfTrim(@Test)) as Test_UDFTrimmed_Length,
	DataLength(dbo.udfTrim(@Test)) as Test_UDFTrimmed_DataLength
	
 
*/
CREATE FUNCTION [dbo].[udfTrim] 
(
	@StringToClean as varchar(8000)
)
RETURNS varchar(8000)
AS
BEGIN	
	--Replace all non printing whitespace characers with Characer 32 whitespace
	--NULL
	Set @StringToClean = Replace(@StringToClean,CHAR(0),CHAR(32));
	--Horizontal Tab
	Set @StringToClean = Replace(@StringToClean,CHAR(9),CHAR(32));
	--Line Feed
	Set @StringToClean = Replace(@StringToClean,CHAR(10),CHAR(32));
	--Vertical Tab
	Set @StringToClean = Replace(@StringToClean,CHAR(11),CHAR(32));
	--Form Feed
	Set @StringToClean = Replace(@StringToClean,CHAR(12),CHAR(32));
	--Carriage Return
	Set @StringToClean = Replace(@StringToClean,CHAR(13),CHAR(32));
	--Column Break
	Set @StringToClean = Replace(@StringToClean,CHAR(14),CHAR(32));
	--Non-breaking space
	Set @StringToClean = Replace(@StringToClean,CHAR(160),CHAR(32));
 
	Set @StringToClean = LTRIM(RTRIM(@StringToClean));
	Return @StringToClean
END
GO
I hope this helps others maintaining old systems which seem to have gremlins.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Israel Cris Valenzuela
Software Developer (Senior) Rain Bird Corporation
United States United States
I am a C# & Sql application developer. I currently work for Rain Bird as a Senior Application Developer and Technical Lead. I run some development projects and have fun learning new technologies.
I teach martial arts at night to kids through the Young Champions of America youth organization.
Follow on   Twitter

Comments and Discussions

 
QuestionVery good article! PinprofessionalVolynsky Alex8-Jan-14 3:16 
GeneralMy Vote of 5 PinmemberKyaw Aung Win17-Dec-13 19:38 
GeneralRe: My Vote of 5 PinmemberIsrael Cris Valenzuela17-Dec-13 21:04 
GeneralMy vote of 5 Pinmembersuesanda31-Jan-13 6:43 
GeneralRe: My vote of 5 PinmemberIsrael Cris Valenzuela31-Jan-13 7:35 
GeneralMy vote of 5 PinmemberCHill608-Nov-12 0:08 
GeneralRe: My vote of 5 PinmemberIsrael Cris Valenzuela31-Jan-13 7:34 
GeneralReason for my vote of 4 very good artical sir Pinmembermanish_sapkal20-Feb-12 23:41 
GeneralReason for my vote of 5 nice one Pinmembernikhi _singh16-Feb-12 17:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 18 Feb 2012
Article Copyright 2012 by Israel Cris Valenzuela
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid