Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS-SQL: trim leading or trailing white space in table rows

Posted on 2004-09-30
8
Medium Priority
?
3,979 Views
Last Modified: 2008-01-09
I have a table with over 4000 rows of data, each with one leading white space.

Q. How can I trim the leading white space in the existing table or insert the data into a new table without the white space???

0
Comment
Question by:kvnsdr
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 336 total points
ID: 12189112
SELECT LTrim(ColA) NewColAName, LTrim(ColB) NewColBName
INTO NewTableName
FROM TableA
0
 
LVL 18

Assisted Solution

by:ShogunWade
ShogunWade earned 332 total points
ID: 12189113
UPDATE MyTable
SET Field1=LTRIM(RTRIM(Field1),
 Field2=LTRIM(RTRIM(Field2)  ...etc...

0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 12189121
well there you have it.   1 solution for each possibility ;)
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 26

Expert Comment

by:Hilaire
ID: 12189176
Also note that you'll have to use varchar/nvarchar datatypes in the new tables,
as char/nchar will keep putting trailing spaces at the end of the strings.

if you use the
"select .... into newtable from oldtable"
syntax to build the new table, use explicit casting :

select cast(ltrim(rtrim(....)) as varchar(<size>) as <fieldname>, .....
into newtable
from oldtable

0
 
LVL 26

Assisted Solution

by:Hilaire
Hilaire earned 332 total points
ID: 12189189
oops missing parenthesis

cast(ltrim(rtrim(....)) as varchar(<size>))
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12189446
I attempting to use the Update method, however I must have the syntax incorrect because (0 row(s) affected)....

UPDATE tableNew
SET col_IP = LTRIM(t1.col_IP)
, col_Dns = LTRIM(t1.col_Dns)
from tableOld t1
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 12189540
the update method should perform a direct update on the same table.

UPDATE tableOld
SET col_IP = LTRIM(tcol_IP)
, col_Dns = LTRIM(tcol_Dns)
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12189935
For future users, here are all the versions and ALL work eceptionaly well, thank you again.....

-- Remove (trim) both leading and trailing white space from data............

select ltrim(rtrim(col_1))col_1,
       ltrim(rtrim(col_2)) col_2
into tbl_New
FROM tbl_Old

-- or --

UPDATE tbl_OLD
SET col_1 = LTRIM(rtrim(col_1))
     , col_2 = LTRIM(rtrim(col_2))

-- or --

select cast(ltrim(rtrim(col_1)) as varchar(50)) as col_1,
         cast(ltrim(rtrim(col_2)) as varchar(100)) as col_2
into tbl_New
from tbl_OLD


0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

971 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