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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Remove spaces and carriage returns from Columns

Author  Topic 

meberg66219
Yak Posting Veteran

65 Posts

Posted - 2010-09-20 : 17:18:55
I have three columns that I am trying to write a statement to remove any leading or following spaces as well as any carriage returns.

All Three columns are varchar
LastName (25 Characters)
FirstName (25 Characters)
Addr1 (255 characters)

I used the following to query for carriage returns or line feed:

select charindex(dbo.syStudent.LastName,char(13)+char(10)) from dbo.syStudent

What is currently happening is when the data from the columns is exported to an .XML the data that has leading or following spaces or un wanted carriage returns is not merging into Outlook for a clean mail merge file.

Now I just need an update query for for char(13), char(10), ltrim and rtrim.

Can anyone help me?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-20 : 17:33:17
Try this (please test first!):

UPDATE YourTable
SET YourColumn = RTRIM(LTRIM(REPLACE(REPLACE(YourColumn, CHAR(10), ''), CHAR(13), '')))


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-20 : 18:39:20
Well...by test I think the SQL Warrior Princess means..

Make a copy of the table, THEN try the update

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx





Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-21 : 01:12:23
Nah, just a SELECT:

SELECT RTRIM(LTRIM(REPLACE(REPLACE(YourColumn, CHAR(10), ''), CHAR(13), '')))
FROM YourColumn

Maybe TOP 1000.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

meberg66219
Yak Posting Veteran

65 Posts

Posted - 2010-09-21 : 09:29:54
Thank you so much for all of the assistance. That seems to have resolved the issue.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-21 : 13:39:01
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -