Solved

MS SQL 2005, replace multi space to 1 space.

Posted on 2010-09-07
9
411 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 500 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Technology Partners: 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!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

740 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