Solved

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

Posted on 2004-09-30
8
3,973 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 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

706 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

18 Experts available now in Live!

Get 1:1 Help Now