2015-08-28 142 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 
相關問題