SQL过滤掉空格和其他特殊字符函数

--SQL过滤掉空格和其他特殊字符函数

crteate function dbo.GetCodeString(@SourceSql varchar(500))
returns nvarchar(500)

as
begin
declare @i int
declare @ValidChar nvarchar(100)
declare @temp nvarchar(500)
set @ValidChar='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'

set @i=0
while @i<=LEN(@SourceSql)
begin

if(charindex(substring(@SourceSql,@i+1,1),@ValidChar))>0
begin
 if(@i=0)
 begin
  set @temp=substring(@SourceSql,@i+1,1)
 end
 else
 begin
  set @temp=@temp+substring(@SourceSql,@i+1,1)
 end
end
set @i=@i+1

end
return @temp
end

 --测试

--select dbo.GetCodeString('ST ror'' US')
--输出结果为STrorUS

原文地址:https://www.cnblogs.com/xqf222/p/3306748.html