Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server replace special character with null

Im unable to replace an special character. Could you please help me on this.

my input is:

Mrs঩Montero

output should be:

Mrs Montero

Special character is not displaying correctly. please see the below image. http://i.stack.imgur.com/b2SdY.jpg

like image 294
srinath Avatar asked May 06 '15 12:05

srinath


1 Answers

Create this function:

CREATE function RemoveSpecialCharacters(@Temp nvarchar(4000))
returns varchar(4000)
as
BEGIN
  DECLARE @KeepValues as varchar(100)
  -- you can add more characters like hyphen if you also want to keep those
  SET @KeepValues = '%[^A-Z0-9 ]%'
  WHILE PatIndex(@KeepValues, @Temp) > 0
    SET @Temp = Stuff(@Temp, PatIndex(@KeepValues, 1, ' ')
  RETURN @Temp
END

Now you can replace the characters.

SELECT dbo.RemoveSpecialCharacters('abc!"¤# ?123')

Result:

abc      123
like image 102
t-clausen.dk Avatar answered Sep 21 '22 17:09

t-clausen.dk