blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Jim 2.0

SQL: Using the Trim function to remove white space from a field

Today I was looking at a table with an apparantly empty column and discovered that the field is actually populated with blank spaces - Fortunately this field is of no interest to me and I noticed it purely by chance when I pasted an extract of the dat into excel and happened to notice the cell was unusually wide when I formatted the sheet. Anyway, it reminded me of an issue that is is often worth looking out for of trailing (or I suppose preceeding) spaces.

 Fortunately this is easily solved with the trim function, which will remove white spaces from a field

SELECT  TRIM(fieldname)
,            LTRIM(fieldname)
,            RTRIM(fieldname)
,            LTRIM(RTRIM(fieldname))
FROM     tablename

Depending on the particular 'flavour' of SQL being used:
TRIM() will remove both leading and trailing white spaces;
LTRIM() removes leading white spaces (i.e. from the beginning of a string);
RTRIM() removes trailing white space (i.e. from the end of a string);
If TRIM() is not supported, then LTRIM(RTRIM()) will achieve the same result.

As I said, this is a useful way to get rid of pesky additional spaces, however just because it can be done, it does not necessarily mean it should be done, since additional functions such as this have a cost in terms of performance to the query. One temptation is to apply the function to every field you select in a statement, but this is not a good idea, instead simply bare it in mind as a useful trick for when it is necessary to remove these white spaces.

 James

Published 14 August 2007 16:19 by James.Pipe

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Mohit Nayyar said:

Yes, Makes sense.....But just remember one more thing........there is a huge difference between empty spaces and NULL...........so while evaluating the result set make sure you check for NULL as well.

You can do this using IsNull or IS NULL or IS NOT NULL in Microsoft SQL Server 2005.

Mohit Nayyar

August 15, 2007 00:48
 

briana said:

I don't get it,

how would I put it in the code of the video?

September 27, 2007 15:20
 

James.Pipe said:

I'm not sure what your question is; what video?

September 28, 2007 13:38
 

Suma said:

How to remove the white spaces in between the words?

October 9, 2007 06:46
 

James.Pipe said:

Try this: replace(@text,' ','')

This replaces any space in a text string with no space

Thanks,

James

October 9, 2007 13:39
 

Erika Aisyah said:

Thank you for your info for truncating all trailing blanks! very useful

SELECT     fieldname

FROM         tablename

WHERE     (LTRIM(RTRIM(fieldname)) = 'Approve')

October 30, 2007 08:37
 

Easton said:

Thank you James.Pipe for the code to remove middle spaces. Very useful!

replace(@text,' ','')

March 25, 2008 01:30
 

Lekshmi said:

Thank you James. It worked for my requirement.

June 17, 2008 10:12
 

GG said:

Great James the code replace(@text,' ','')  it's what i was looking for.

Thanks a lot!!!

June 18, 2008 15:20
 

Abhinandan Singh said:

I have to remove space from the string '8000002008Q1 0001'.Space is at the 13th spot . I m working on d2k.

June 23, 2008 12:46
 

Abhinandan Singh said:

I have to remove space from the string '8000002008Q1 0001'.Space is at the 13th spot . I m working on d2k. Please it is urgent

June 23, 2008 12:46
 

Mogamad Allie said:

Hi Abhinandan

The code replace above your insert will still work eg

select replace('8000002008Q1 0001',' ','')

Will retrun the string without the space.

July 2, 2008 16:41

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems