• Sergiy (8/24/2016)


    There was an article from Jeff Moden how to replace any number of consecutive spaces with a single one without recurring REPLACE calls.

    I don't have it handy, but it goes like this:

    DECLARE @ReplaceChar NCHAR(1)

    SET @ReplaceChar = CHAR(7) -- any character you sure is not mentioned anywhere in the strings

    SELECT s.Sometext, REPLACE(REPLACE(REPLACE(s.Sometext, ' ', ' ' + @ReplaceChar), @ReplaceChar + ' ', ''), @ReplaceChar, '')

    FROM #Sample s

    The code that I included is from the discussion from that article. It's mentioned to be the fastest method proposed.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2