SQL Function to replace special characters from a string
CREATE Function [dbo].[replaceSpecialCharacters] (@str varchar(256)) returns varchar(256)
with schemabinding
begin
SET @str = LTRIM(RTRIM(@str))
DECLARE @expres VARCHAR(50) = '%[~,@,#,$,%,&,*,{,},(,),.,!,:,?,^,+,=,;,-,/,\]%'
WHILE PATINDEX( @expres, @str ) > 0
BEGIN
SET @str = REPLACE(@str, SUBSTRING( @str, PATINDEX( @expres, @str ), 1 ),'_')
END
SET @str = Replace(@str,' ','||')
SET @str = Replace(@str,'’','')
SET @str = Replace(@str,'_','')
SET @str = Replace(@str,'||','_')
SET @str = Replace(@str,'__','_')
SET @str = Replace(@str,'__','_')
SET @str = LTRIM(RTRIM(@str))
return @str
END
GO
with schemabinding
begin
SET @str = LTRIM(RTRIM(@str))
DECLARE @expres VARCHAR(50) = '%[~,@,#,$,%,&,*,{,},(,),.,!,:,?,^,+,=,;,-,/,\]%'
WHILE PATINDEX( @expres, @str ) > 0
BEGIN
SET @str = REPLACE(@str, SUBSTRING( @str, PATINDEX( @expres, @str ), 1 ),'_')
END
SET @str = Replace(@str,' ','||')
SET @str = Replace(@str,'’','')
SET @str = Replace(@str,'_','')
SET @str = Replace(@str,'||','_')
SET @str = Replace(@str,'__','_')
SET @str = Replace(@str,'__','_')
SET @str = LTRIM(RTRIM(@str))
return @str
END
GO
Comments
Post a Comment