Click here to Skip to main content
11,922,641 members (57,491 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked

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))


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
@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)
	@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)
	--Replace all non printing whitespace characers with Characer 32 whitespace
	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
I hope this helps others maintaining old systems which seem to have gremlins.


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


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.

You may also be interested in...

Comments and Discussions

QuestionVery good article! Pin
Volynsky Alex8-Jan-14 4:16
professionalVolynsky Alex8-Jan-14 4:16 
GeneralMy Vote of 5 Pin
Kyaw Aung Win17-Dec-13 20:38
memberKyaw Aung Win17-Dec-13 20:38 
GeneralRe: My Vote of 5 Pin
Israel Cris Valenzuela17-Dec-13 22:04
memberIsrael Cris Valenzuela17-Dec-13 22:04 
GeneralMy vote of 5 Pin
suesanda31-Jan-13 7:43
membersuesanda31-Jan-13 7:43 
GeneralRe: My vote of 5 Pin
Israel Cris Valenzuela31-Jan-13 8:35
memberIsrael Cris Valenzuela31-Jan-13 8:35 
GeneralMy vote of 5 Pin
CHill608-Nov-12 1:08
memberCHill608-Nov-12 1:08 
GeneralRe: My vote of 5 Pin
Israel Cris Valenzuela31-Jan-13 8:34
memberIsrael Cris Valenzuela31-Jan-13 8:34 
GeneralReason for my vote of 4 very good artical sir Pin
manish_sapkal21-Feb-12 0:41
membermanish_sapkal21-Feb-12 0:41 
GeneralReason for my vote of 5 nice one Pin
nikhi _singh16-Feb-12 18:59
membernikhi _singh16-Feb-12 18:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.151125.1 | Last Updated 18 Feb 2012
Article Copyright 2012 by Israel Cris Valenzuela
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid