1

我们正在尝试使用下面的(简化的)命令从字符串中去除某些特殊字符,这是我们在搜索后看到的最常见的解决方案。但是,使用某些特殊字符时结果不一致。谁能解释为什么?而且,更好的是,任何人都可以提供有效的解决方案吗?

SQL Server 2014

在下面的第一种情况下,“@”被删除,但在包含它的所有其他情况下 (2+5),它不会被删除。第三种情况相同:删除空格,但不删除 '&'; 在第 5 种情况下,空格被删除,但不是“@”。其他组合也有类似的问题。

谢谢。声明 @str varchar(50) = '1st Ave @ 1st St FL-3 Rm 323& New York NY'

declare @Pindex1  varchar(10) = '%[@]%'
declare @Pindex2  varchar(10) = '%[@& ]%'
declare @Pindex3  varchar(10) = '%[& ]%'
declare @Pindex4  varchar(10) = '%[ ]%'
declare @Pindex5  varchar(10) = '%[@ ]%'

Select @str as String, @Pindex1 as Pattern ,Replace(@str, Substring(@str, PatIndex(@Pindex1,@str), 1), '') as PIndex1_result
Select @str as String, @Pindex2 as Pattern ,Replace(@str, Substring(@str, PatIndex(@Pindex2,@str), 1), '') as PIndex2_result
Select @str as String, @Pindex3 as Pattern ,Replace(@str, Substring(@str, PatIndex(@pindex3,@str), 1), '') as PIndex3_result
Select @str as String, @Pindex4 as Pattern ,Replace(@str, Substring(@str, PatIndex(@Pindex4,@str), 1), '') as PIndex4_result
Select @str as String, @Pindex5 as Pattern,Replace(@str, Substring(@str, PatIndex(@pindex5,@str), 1), '') as PIndex5_result
4

1 回答 1

1

我认为您可能对 SQL Server 模式有误解。考虑第二种模式:

declare @Pindex2  varchar(10) = '%[@- ]%'

这不能匹配任何模式。为什么?的 ASCII 值为'@'64,空格为 32。这些值之间没有任何内容。它类似于'%[b-a]%',也不会匹配任何内容。

我认为问题在于您对 SQL Server 字符串模式的理解。

于 2017-05-15T17:22:53.137 回答