Im unable to replace an special character. Could you please help me on this.
my input is:
MrsMontero
output should be:
Mrs Montero
Special character is not displaying correctly. please see the below image.
Im unable to replace an special character. Could you please help me on this.
my input is:
MrsMontero
output should be:
Mrs Montero
Special character is not displaying correctly. please see the below image.
If you're simply selecting then do:
Select Replace(textct, Special_char, ' ')
from mytable
If you're updating then do this:
Update mytable
Set textct = Replace(textct, Special_char, ' ')
Assuming these are nvarchars then do this:
Select Replace(cast(textct as varchar),cast(Special_char as varchar), ' ')
from mytable
If you want to remove all special characters you will need to use a function like this:
Create Function RemoveSpecialCharacters (@text nvarchar(max))
Returns varchar(4000)
AS
BEGIN
Declare @Return varchar(4000) = Cast(@text as varchar(4000))
While PatIndex('%[^a-z ]%', @Return) > 0
Set @Return = Stuff(@Return, PatIndex('%[^a-z ]%', @text), 1, ' ')
Return @Return
END
Select RemoveSpecialCharacters(textct) from mytable
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
From the source you can create a function like this:
create function dbo.RemoveSpecialChars (@s varchar(256)) returns varchar(256)
with schemabinding
begin
if @s is null
return null
declare @s2 varchar(256)
set @s2 = ''
declare @l int
set @l = len(@s)
declare @p int
set @p = 1
while @p <= @l begin
declare @c int
set @c = ascii(substring(@s, @p, 1))
if @c between 48 and 57 or @c between 65 and 90 or @c between 97 and 122
set @s2 = @s2 + char(@c)
set @p = @p + 1
end
if len(@s2) = 0
return null
return @s2
end
And if you have only that special character then you can use this:
SELECT replace('MrsMontero', '', ' ')
You can
select
textct,
replace(textct collate Latin1_General_BIN, nchar(2473), ' ')
from
mytable