Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Remove trailing spaces at the end
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

maddyslayer
Yak Posting Veteran

57 Posts

Posted - 05/15/2013 :  12:53:01  Show Profile  Reply with Quote
I have a column called 'name' in table 'persons'. How do I remove the spaces at the end? I tried rtrim, but it doesn't work.

maddyslayer
Yak Posting Veteran

57 Posts

Posted - 05/15/2013 :  12:54:52  Show Profile  Reply with Quote
I tried,

UPDATE persons
SET name = RTRIM(name)

But it didn't work.
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
352 Posts

Posted - 05/15/2013 :  13:59:46  Show Profile  Reply with Quote
Is the column CHAR or VARCHAR?

djj
Go to Top of Page

maddyslayer
Yak Posting Veteran

57 Posts

Posted - 05/15/2013 :  14:27:11  Show Profile  Reply with Quote
the column is varchar
Go to Top of Page

ScottPletcher
Aged Yak Warrior

USA
550 Posts

Posted - 05/15/2013 :  14:44:15  Show Profile  Reply with Quote
Then they're not actually spaces. They're like a CR (char(10)) or LF (char(13)) or Tab (char(9)) or some other non-space but non-printable character.


UPDATE persons
SET name = RTRIM(REPLACE(REPLACE(REPLACE(name, CHAR(9), ''), CHAR(10), ''), CHAR(13), ''))


Edited by - ScottPletcher on 05/15/2013 14:45:31
Go to Top of Page

maddyslayer
Yak Posting Veteran

57 Posts

Posted - 05/15/2013 :  15:08:40  Show Profile  Reply with Quote
What is the select statement to get the list of names that are like that?
Go to Top of Page

ScottPletcher
Aged Yak Warrior

USA
550 Posts

Posted - 05/16/2013 :  12:23:08  Show Profile  Reply with Quote
Sorry, my bad, I mistakenly left that part off:


UPDATE persons
SET name = RTRIM(REPLACE(REPLACE(REPLACE(name, CHAR(9), ''), CHAR(10), ''), CHAR(13), ''))
WHERE
name LIKE '%' + CHAR(09) + '%' OR --tab
name LIKE '%' + CHAR(10) + '%' OR --lf
name LIKE '%' + CHAR(13) + '%' --cr


SELECT *
FROM persons
WHERE
name LIKE '%' + CHAR(09) + '%' OR --tab
name LIKE '%' + CHAR(10) + '%' OR --lf
name LIKE '%' + CHAR(13) + '%' --cr
Go to Top of Page

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1834 Posts

Posted - 05/16/2013 :  12:31:16  Show Profile  Reply with Quote
select name
from persons
where right(name, 1) in (char(9), char(10), char(13))

There might be other non-printable characters in addition to TAB, CR and LF. You would need to expand the elements of the IN list. Alternately, instead of looking for a non-printable character, you could look for a character which not a printable character
select name
from persons
where right(name, 1) not like '[a-zA-Z0-9,./!@#$%^&*()_+-={}\|;:"'']'


=================================================
I am not one of those who in expressing opinions confine themselves to facts. (Mark Twain)
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.33 seconds. Powered By: Snitz Forums 2000