2015-08-28 146 views
1

我试图用sql中的单个空格替换多个空格和回车符。 我想出迄今以下:如何用sql中的单个空格替换多个空格和回车符

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 

'|','') 

现在对于上面的字符串预期的输出结果是: 6室6个空间。 abcde

但是我得到6个空间6个空间。 [多个空格] abcde(Stackoverflow正在修整这里的空白处,我必须写出它) 这似乎是我的一个难题。 有什么问题?

+0

你的预期和实际看起来是一样的,我想你需要澄清这一点。 –

+1

请问您可以添加一个漂亮的(!)代码的格式化版本吗?目前,甚至很难编辑你的问题,使代码看起来漂亮和可读(例如“在此输入代码”)。 –

+0

@ChristianBarron回车正在被多个空格而不是单个空格所替代。 Stackoverflow正在修改我的问题和评论中的空格,请注意预期的和实际的外观相同 –

回答

2

那么我只是把它放在那里作为替代,因为我刚刚完成了第二个答案被接受。

这也会给你你想要的结果,通过修剪,并在正确的顺序更换:

Select Replace(replace(replace(replace(
             RTRIM(LTRIM(this)), 
             char(13) + char(10), ''), 
             ' ', ' |'), 
             '| ', ''), 
             '|','') 
from 
(select '  6  Spaces 6  Spaces.  
      abcde ' as this) a 
0

这种类型的问题是棘手的简单解决的替换功能,但变得非常容易与正则表达式的功能。

不幸的是,微软并没有把它作为SQL Server的内置函数,但是有一些SQLCLR工作可用。

SQL Server Regular expressions in T-SQL有SQLCLR功能的示例搜索字符串,但在这里,你将需要一个regex_replace函数

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 

    } 
} 

一经面市就可以实现与类似下面的查询您的结果;

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

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

返回

字符串有许多奇怪的间距问题。 !

表达式\ s {1,}表示匹配一个或多个{1,}序列中的任何空格\ s,并且匹配将被替换为您的单个空格字符。

使用SQLCLR比使用此处包含的代码还有更多,并且需要进一步研究创建程序集和SQLCLR函数。

0

你可以试试下面的代码:

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 
相关问题