Welcome to EMC Consulting Blogs Sign in | Join | Help

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
 

Lost in space said:

If I am updating the table and I want to replace '' with NULL do I have to use a CASE? (CASE WHEN ltrim(rtrim([Field])) = '' THEN NULL

August 22, 2008 21:26
 

NADEEM said:

MANY MANY THANKS

September 9, 2008 12:17
 

k_lib said:

can you do something like this?

 select TRIM(*)

from DB

I know it won't work but i didn't know if maybe there was a easy way.

December 11, 2008 17:46
 

Saravanan said:

mghjghjghj

April 21, 2009 10:59
 

Marla said:

OK, for Abhinandan Singh it seems pretty straight forward, go into the cell and delete the 13th spot yourself, but does anyone know how to remove any blanks not just within once cell, but within a whole column with different data?

Thanks

April 28, 2009 17:45
 

Nigel said:

UPDATE table_name

SET field_name=REPLACE(REPLACE(REPLACE(field_name,char(9),''),char(10),''),char(13),'')

To remove returns (13), new lines (10) and tabs (9). Quite slow but it was only run once to correct something.

May 5, 2009 14:56
 

kEL said:

replace(@TEXT,' ','')

THANKS, PERFECT!

November 23, 2009 17:28
 

Ashish Srivastava said:

Thanks Friends .....

It Gave me Help and i solved my problem......Thaks Again

December 24, 2009 06:04
 

Gopinath Subbaian said:

Thanks James

June 29, 2010 06:58
 

prasad said:

Thanks great article keep it up

July 19, 2010 13:55
 

Dao said:

Hi ,

Thats a great article , but I have one more question; how to eliminate the wite spaces of all fields should I

select TRIM(*) from xyz ;

Thankns for your help

September 1, 2010 08:52
 

SQL User said:

Thanks James this came in to be very handy for me !

September 22, 2010 18:49
 

jeffry said:

how to remove blank space n between words ir sentece is

i 'want' to 'remove' space in pl/sql

November 23, 2010 14:12
 

JOHN MANAVALAN said:

How to replace 'X           X                     X                  X' to 'X X X X'

May 26, 2011 13:33
 

Simon G said:

Hi John

Had the same problem and solved it with this function:

Create

function dbo.RemoveSpaceBetween (@strInput nvarchar(max))

Returns nvarchar(max)

As

Begin

declare @strOutput nvarchar(max)

set @strOutput = @strInput

while CHARINDEX ('  ', @strOutput) > 0

set @strOutput = replace( @strOutput,'  ', ' ')

return @strOutput

End

Go

Select dbo.RemoveSpaceBetween('sss              ff  fff')

August 8, 2011 10:13
 

John said:

Please could you help me with code to remove all spaces at the beginning and end of every column. For example;

col1       col2       col3       col4

John      Brook     16         New drive

Ice        Cook       3          Bincroft Garden

should be

JohnBrook16New drive

IceCook3Bincroft Garden

Thanks in advance

September 30, 2011 10:01
 

Balu said:

Hi John,

   Use trim function to remove white space in start and end position and use pipe symbol for concatenation.

 eg: select trim(col1)||trim(col2)||trim(col3)||trim(col4) from tablename

October 20, 2011 13:09
 

Balu said:

Hi JOHN MANAVALAN,

 Replace 'X           X                     X                  X' to 'X X X X'

Try this..

SELECT REGEXP_REPLACE('X           X                     X                  X', '( ){2,}', ' ') RESULT FROM dual;

October 20, 2011 13:14
 

Shoki said:

Thank you!! The replace(@text,' ','') worked like a charm!!!!

February 28, 2012 12:30

Leave a Comment

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