We are trying to strip certain special characters from a string using the (simplified) command below, which is the most common solution we've seen after searching. But, the results are inconsistent when using certain special characters. Can anyone explain why? And, better, can anyone offer a solution that works?
SQL Server 2014
In the first case below, the '@' is removed, but in all the other cases where it is included (2+5) it is not removed. Same for the 3rd case: the spaces are removed, but not the '&'; and in the 5th case, the spaces are removed, but not the '@'. Other combinations have similar issues.
Thanks. declare @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