1

I'm trying to replace multiple white spaces and carriage return with a single white space in sql. I've come up with following so far:

select  
replace(

replace(

replace(

replace(

replace(

replace(

LTrim(RTrim('      6      Spaces  6      Spaces.     
            abcde ')),char(13),''),--Trim the field
enter code here
                                                char(10),''),
char(13) + char(10),''),

 '  ',' |'),                                            
--Mark double spaces

  '| ',''),                                                 
--Delete double spaces offset by 1

'|','')  

Now the expected output for the above string is: 6 Spaces 6 Spaces. abcde

However I'm getting 6 Spaces 6 Spaces. [multiple white spaces] abcde (Stackoverflow is trimming the white spaces here, ergo I have to write it) This seems like a conundrum to me. What's wrong?

Ritesh Bhakre
  • 203
  • 3
  • 10
  • Your expected and actual looks the same, I think you need to clarify this a bit. – Christian Barron Aug 28 '15 at 08:43
  • 1
    Could you please add a pretty (!) formated version of your code? At the moment it is very hard to even edit your question to make the code look pretty and readable (e.g. "enter code here" ???). – Ocaso Protal Aug 28 '15 at 08:45
  • @ChristianBarron carriage return is being replaced by multiple white spaces instead of a single white space. Stackoverflow is trimming the white spaces in my questions and comments, ergo the expected and actual look the same – Ritesh Bhakre Aug 28 '15 at 09:21

3 Answers3

2

Well I'll just put this out there as an alternative since I just finished it up the second an answer was accepted.

This will also give you the results you desired by trimming and replacing in the correct orders:

Select Replace(replace(replace(replace(
                                       RTRIM(LTRIM(this)), 
                                       char(13) + char(10), ''),
                                       '  ', ' |'),
                                       '| ', ''),
                                       '|','')
from
(select '      6      Spaces  6      Spaces.     
            abcde ' as this) a
Christian Barron
  • 2,695
  • 1
  • 14
  • 22
0

This type of problem is tricky to solve with simple replace functionality but becomes very easy with a regex function.

Sadly Microsoft haven't included this as a built in function for SQL Server but with some SQLCLR work it can be available.

SQL Server Regular expressions in T-SQL has an example of a SQLCLR function to search strings but here you would need a regex_replace function

using System.Data.SqlTypes;

namespace Public.SQLServer.SQLCLR
{
    public class Regex
    {

        #region Regex_IsMatch Function
        /// <summary>
        ///     Searches an expression for another regular expression and returns a boolean value of true if found.
        /// </summary>
        /// <param name="expressionToFind">Is a character expression that contains the sequence to be found. This expression leverages regular expression pattern matching syntax. This expression may also be simple expression.</param>
        /// <param name="expressionToSearch">Is a character expression to be searched.</param>
        /// <param name="start_location">Is an integer expression at which the search starts. If start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expressionToSearch.</param>
        /// <returns>Bit.</returns>
        [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true)]
        public static SqlBoolean Regex_IsMatch(SqlString expressionToFind, SqlString expressionToSearch, SqlInt32 start_location)
        {
            // Process expressionToFind parameter
            string etf;

            if (expressionToFind.IsNull)
            {
                return SqlBoolean.Null;
            }
            else if (expressionToFind.Value == string.Empty)
            {
                return new SqlBoolean(0);
            }
            else
            {
                etf = expressionToFind.Value;
            }

            // Process expressionToSearch parameter
            string ets;

            if (expressionToSearch.IsNull)
            {
                return SqlBoolean.Null;
            }
            else if (expressionToSearch.Value == string.Empty)
            {
                return new SqlBoolean(0);
            }
            else
            {
                ets = expressionToSearch.Value;
            }

            // Process start_location parameter
            int sl;

            if (start_location.IsNull)
            {
                sl = 0;
            }
            else if (start_location.Value < 1)
            {
                sl = 0;
            }
            else
            {
                sl = (int)start_location.Value -1;
                if (sl > expressionToSearch.Value.Length + 1)
                {
                    sl = expressionToSearch.Value.Length;
                }
            }


            // execute the regex search
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(etf);

            return regex.IsMatch(ets, sl);

        }
        #endregion

        #region Regex_Replace Function
        /// <summary>
        ///     Replaces all occurrences of a specified regular expression pattern with another regular expression substitution.
        /// </summary>
        /// <param name="expression">Is the string expression to be searched.</param>
        /// <param name="pattern">Is a character expression that contains the sequence to be replaced. This expression leverages regular expression pattern matching syntax. This expression may also be simple expression.</param>
        /// <param name="replacement">Is a character expression that contains the sequence to be inserted. This expression leverages regular expression substitution syntax. This expression may also be simple expression.</param>
        /// <returns>String of nvarchar(max), the length of which depends on the input.</returns>
        [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true)]
        public static SqlString Regex_Replace(SqlString expression, SqlString pattern, SqlString replacement)
        {

            // Process null inputs
            if (expression.IsNull)
            {
                return SqlString.Null;
            }
            else if (pattern.IsNull)
            {
                return SqlString.Null;
            }
            else if (replacement.IsNull)
            {
                return SqlString.Null;
            }

            // Process blank inputs
            else if (expression.Value == string.Empty)
            {
                return expression;
            }
            else if (pattern.Value == string.Empty)
            {
                return expression;
            }

            // Process replacement parameter

            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern.Value);

            return regex.Replace(expression.Value, replacement.Value);

        }
        #endregion

    }
}

Once available you can achieve your result with a query like the following;

select [library].[Regex_Replace]('String  with  many        odd spacing
    issues.

        !','\s{1,}',' ')

which returns

String with many odd spacing issues. !

the expression \s{1,} means match any whitespace \s in a sequence of one or more {1,} and matches are replaced with your single space character.

There is more to using SQLCLR than the code included here and further research into creating the assemblies and SQLCLR functions is required.

Community
  • 1
  • 1
Edward Comeau
  • 3,874
  • 2
  • 21
  • 24
0

You can try the below code :

select top 10 Note, LEN(Note) AS Before, LEN(replace(replace(replace(Note,' ','<>'),'><',''),'<>',' ') ) AS After,
 replace(replace(replace(Note,' ','<>'),'><',''),'<>',' ') as Note  from #ClientNote
 WHERE note LIKE '%  %'
 order by DATALENGTH(Note) desc
Robert
  • 5,278
  • 43
  • 65
  • 115
Peter
  • 663
  • 1
  • 7
  • 15