从MS SQL中的字符串中删除不需要的字符

时间:2011-05-09 13:36:01

标签: sql sql-server tsql

下面有一些代码返回一个干净的字符串

如何在输出中保留逗号

select @cleanline=line from #tbl

    While PatIndex('%[^A-Z,a-z,0-9]%', @cleanline) > 0         
    Set @cleanline = Stuff(@cleanline, PatIndex('%[^A-Z,a-z,0-9]%', @cleanline), 1, '') 

    print @cleanline

由于 罗布

1 个答案:

答案 0 :(得分:3)

%[^A-Z,a-z,0-9]%更改为%[^A-Z0-9]%%[^A-Za-z0-9]%

默认情况下,SQL Server不区分大小写,您不需要pattern-match strings中的逗号

编辑:结果是数据中有没有逗号...

select @cleanline = line from #tbl
set @tokeeppattern = '%[^A-Z,a-z0-9' + CHAR(9) + ']%'


While PatIndex(@tokeeppattern, @cleanline) > 0         
Set @cleanline = Stuff(@cleanline, PatIndex(@tokeeppattern, @cleanline), 1, '') 

print @cleanline