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

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2012-09-13 : 12:53:25
Hi there,

I have an ID_Column. The matter is that the data is as follows:

AXBN- JT3

AXBN-JT3



I have some middle spaces in some cells. I need to remove those spaces, because SQL is recognizing the cells as a different one, due to that middle space.

Anyone knows any query to remove it?

Many thanks

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-09-13 : 12:56:33
Use the replace function to remove the spaces.

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

Subscribe to my blog
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-09-13 : 13:02:40
How you will write the query?

Using Monthly_Table and Mydatabase.

Regards
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-09-13 : 13:06:18
I used the following query to remove the left and right spaces. But i cant remove the middle ones:

UPDATE table
SET ax = replace ( ax, ax, RTRIM(ltrim( ax)))
where ax <>''

Thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-09-13 : 13:18:23
Why did you add RTRIM/LTRIM to the query?

UPDATE t1
SET c1 = REPLACE(c1, ' ', '')

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

Subscribe to my blog
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2012-09-13 : 13:32:16
Perfect.

I used them, because, I'd take info from other columns and they had spaces too.

Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-09-13 : 13:41:03
What I posted doesn't require RTRIM/LTRIM at all. It removes ALL spaces from that column, regardless of where they are.

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 -