Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS SQL 2005, replace multi space to 1 space.

Posted on 2010-09-07
9
Medium Priority
?
416 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
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

963 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