我正在尝试用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个Spaces 6个空格。 [多个空格] abcde(Stackoverflow正在修剪白色空格,我必须写它) 这对我来说似乎是一个难题。 怎么了?
答案 0 :(得分:2)
好吧,我只是把它作为替代方案,因为我刚刚完成了第二个答案被接受了。
这也将通过修剪和替换正确的顺序为您提供所需的结果:
Select Replace(replace(replace(replace(
RTRIM(LTRIM(this)),
char(13) + char(10), ''),
' ', ' |'),
'| ', ''),
'|','')
from
(select ' 6 Spaces 6 Spaces.
abcde ' as this) a
答案 1 :(得分: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,}序列中的任何空格,并且匹配将替换为您的单个空格字符。
使用SQLCLR比使用此处包含的代码还要多,并且需要进一步研究创建程序集和SQLCLR函数。
答案 2 :(得分:0)
您可以尝试以下代码:
<Window x:Class="View.SingleLineTextMode"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SingleLineTextMode" Height="300" Width="300" WindowState="Maximized" WindowStyle="None" WindowStartupLocation="CenterScreen" Background="Black">
<Window.Resources>
<Style x:Key="MyStyle" BasedOn="{x:Null}" TargetType="{x:Type RichTextBox}">
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="Padding" Value="0,5,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RichTextBox}">
<Border x:Name="bg" BorderBrush="Yellow" BorderThickness="3,3,0,3" Background="{TemplateBinding Background}">
<ScrollViewer x:Name="PART_ContentHost" IsDeferredScrollingEnabled="True" CanContentScroll="False" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="Yellow"/>
<Setter Property="BorderThickness" TargetName="bg" Value="3,3,0,3"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" TargetName="bg" Value="Yellow"/>
<Setter Property="BorderThickness" TargetName="bg" Value="3,3,0,3"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<!--<RichTextBox Name="txtAppendValue" Height="60" Width="1920" Style="{StaticResource MyStyle}" Margin="0,0,-10,0" FontSize="60" FontFamily="Arial" IsReadOnly="True" Focusable="False" Cursor="Arrow" BorderThickness="3,3,3,3" IsUndoEnabled="False" UndoLimit="0" TextOptions.TextFormattingMode="Display" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.CacheLength="2,3" VirtualizingPanel.CacheLengthUnit="Page" TextBlock.LineHeight="100" Padding="0">
</RichTextBox>-->
<RichTextBox Name="txtAppendValue" HorizontalAlignment="Center" VerticalAlignment="Center" Height="60" Width="1920" Style="{StaticResource MyStyle}" Margin="0,0,0,0" FontSize="40" FontFamily="Arial" IsReadOnly="True" Focusable="False" Cursor="Arrow" IsUndoEnabled="False" UndoLimit="0" TextOptions.TextFormattingMode="Display" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.CacheLength="2,3" VirtualizingPanel.CacheLengthUnit="Page" TextBlock.LineHeight="100" >
</RichTextBox>
<Label Name="lblStatusUpdate" ></Label>
</Grid>
</Window>