Solved

SQL Server remove trailing spaces in table fields

Posted on 2012-04-04
7
449 Views
Last Modified: 2012-04-05
Is there a way to remove the trailing spaces in fields in a sql server table? I don't mean using rtrim or something like that for viewing. I want to permanently remove any trailing white space in the fields of various tables. I have a lot of records and fields, so doing it one record at a time manually is not possible. I'm trying to find out if it is possible. I'm using SQL Server 2005. Thanks.
0
Comment
Question by:dodgerfan
7 Comments
 
LVL 6

Accepted Solution

by:
Patrick Tallarico earned 500 total points
ID: 37808625
can you update the fields in the table using rtrim?

Update table
set column = rtrim(column),
     column2=rtrim(column2)
0
 
LVL 42

Expert Comment

by:dqmq
ID: 37808632
update yourtable
   set
     yourcolumn1 = rtrim(yourcolumn1)
  ,  yourcolumn2 = rtrim(yourcolumn2)


The above could be automated across a table, database, or server, but it is probably easier to do just do it manually with copy/paste into a TSQL script.
0
 
LVL 27

Expert Comment

by:tliotta
ID: 37808717
If you want to exclude something like rtrim(), then I don't understand what you mean.

That is, I would use an UPDATE statement that set the value of a column to be the value of rtrim(<column>).

Of course, that assumes that the column is VCHAR in the first place. If not, then I'd alter the column data type first; then I'd UPDATE to set the values.

Can you clarify if you're excluding rtrim() or if it's just the use of rtrim() for retrieval/viewing?

Tom
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 6

Expert Comment

by:Patrick Tallarico
ID: 37808738
I could be wrong, but I think @dodgerfan was speaking only of the fact that when used in a View, rtrim, doesn't affect a change onto the actual table.  When used in an update statement, rtrim() should work just fine for @dodgerfan.

@dodgerfan, please let me know if I am off on this train of thought.
0
 
LVL 27

Expert Comment

by:tliotta
ID: 37809022
Yep, we probably see it the same way. But every once in a while, someone asks a question like this and actually has a surprising reason behind it. I usually learn something from those reasons. I've turned a little cautious just in case.

Tom
0
 
LVL 6

Expert Comment

by:Patrick Tallarico
ID: 37809093
@tliotta, I definitely agree.  I wasn't intending to downplay your comment.  In addition, it's a good comment to check the data type.  I often forget to ask that question.  Thanks for the reminder.
0
 

Author Comment

by:dodgerfan
ID: 37812238
I apologize for not getting back to this earlier, and for the confusion. I was speaking of using RTRIM in a view. I have no idea why I never thought of using it in an update statement. I've already tested it and it works great. Thanks for help.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sql Permission 6 52
Parsing the XML data to SQL Server 4 59
How to order Highest and lowest value rows alternatively in SQL Server ? 4 44
Restrict result set 1 33
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now