Solved

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

Posted on 2004-09-30
8
3,977 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 15

Accepted Solution

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

Assisted Solution

by:ShogunWade
ShogunWade earned 83 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 83 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

628 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