Click here to Skip to main content
15,398,501 members
Articles / Programming Languages / T-SQL
Posted 16 Feb 2012


1 bookmarked

LTRIM RTRIM doesn’t always work

Rate me:
Please Sign up or sign in to vote.
4.92/5 (10 votes)
18 Feb 2012CPOL1 min read
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)
United States United States
I am a full stack software engineer and architect with the majority of my experience on the Microsoft Stack. I teach martial arts for a non-profit organization.

Comments and Discussions

QuestionVery good article! Pin
Volynsky Alex8-Jan-14 3:16
professionalVolynsky Alex8-Jan-14 3:16 
GeneralMy Vote of 5 Pin
Kyaw Aung Win17-Dec-13 19:38
MemberKyaw Aung Win17-Dec-13 19:38 
GeneralRe: My Vote of 5 Pin
Israel Cris Valenzuela17-Dec-13 21:04
MemberIsrael Cris Valenzuela17-Dec-13 21:04 
GeneralMy vote of 5 Pin
suesanda31-Jan-13 6:43
Membersuesanda31-Jan-13 6:43 
GeneralRe: My vote of 5 Pin
Israel Cris Valenzuela31-Jan-13 7:35
MemberIsrael Cris Valenzuela31-Jan-13 7:35 
GeneralMy vote of 5 Pin
CHill608-Nov-12 0:08
mveCHill608-Nov-12 0:08 
GeneralRe: My vote of 5 Pin
Israel Cris Valenzuela31-Jan-13 7:34
MemberIsrael Cris Valenzuela31-Jan-13 7:34 
GeneralReason for my vote of 4 very good artical sir Pin
manish_sapkal20-Feb-12 23:41
Membermanish_sapkal20-Feb-12 23:41 
GeneralReason for my vote of 5 nice one Pin
Nikhil_S16-Feb-12 17:59
professionalNikhil_S16-Feb-12 17: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.