Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS SQL 2005, replace multi space to 1 space.

Posted on 2010-09-07
9
Medium Priority
?
415 Views
Last Modified: 2012-05-10
The MS SQL server 2005 stores some data: "this is        a                   test              for  data"
and now I want to convert them to: "this is a test for data"

How can I do it?
0
Comment
Question by:JameMeck
[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
9 Comments
 
LVL 6

Accepted Solution

by:
apresence earned 2000 total points
ID: 33615671
Take a look at:
http://www.sqlhacks.com/Retrieve/Remove-Blanks

For example:
select ltrim(replace(replace(replace('this                      is         a       test', '  ', ' `````'), '````` ',''),'`````',''))

Returns:
this is a test
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33615672
use REPLACE(YourString, '  ',' ')

0
 
LVL 6

Expert Comment

by:apresence
ID: 33615684
VjSoft:
The replace is not recursive.  Using your example:
replace('this                      is         a       test', '  ', ' ')

Produces this output:
this           is     a    test
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33615712
@apresence, I only provide hint, if you need code example here it is

SELECT REPLACE(REPLACE('this                      is         a       test','  ','-'),'-','')
0
 
LVL 6

Expert Comment

by:apresence
ID: 33615738
Query:
SELECT REPLACE(REPLACE('this                      is         a       test','  ','-'),'-','')

Output:
thisis a test
0
 
LVL 2

Expert Comment

by:cetafudd
ID: 33616672
I found this a while ago and am using it -- it works great

DECLARE @OriginalString VARCHAR(100)

SET @OriginalString = 'this is        a                   test              for  data'


SELECT
REPLACE(          
      REPLACE(
            REPLACE(
                  LTRIM(RTRIM(@OriginalString))
                        ,'  ',' '+CHAR(7))  
                  ,CHAR(7)+' ','')        
            ,CHAR(7),'') AS CleanString
-- FROM yourtable (instead of using @originalString variable)
WHERE CHARINDEX('  ',@OriginalString) > 0
0
 
LVL 11

Expert Comment

by:Larissa T
ID: 33617759
This one trims all type of "spaces", not just  plain char(32)


create FUNCTION fVal_trimSpaces
      (@name varchar(8000)
        )
RETURNS varchar(8000)
AS
BEGIN
      select @name = replace(replace(replace(replace(replace(replace(replace(replace
            (@name,'      ' ,' ')
                  ,char(160),' ')
                  ,char(32),' ')
                  ,char(9),' ')
                  ,char(10),' ')
                  ,char(11),' ')
                  ,char(12),' ')
                  ,char(13),' ')
       while (charindex('  ', @name) > 0)
            select  @name =  replace(@name,'  ', ' ')

      select @name = rtrim(ltrim(@name))
      return @name

END
0
 
LVL 5

Expert Comment

by:Vipul Patel
ID: 33620499
I would prefer to write CLR function for better performance.
Please visit http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
0
 

Author Closing Comment

by:JameMeck
ID: 33643697
Thanks! It is my solution.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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
Viewers will learn how the fundamental information of how to create a table.

715 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