I have 1 field which contains special characters in the data, and i want to remove special character and need to keep on A to Z, a to z, 0-9 and few special character allowed. I tried to use simple SQL query so that i can update column using update query using regular expression. But i noticed this is not working in replace function.
Regular expression worked only in WHERE clause to filter the records. So i have only way to create function to remove special characters from the string.
Create below function in database and use it to remove special character from string. Feel free to modify this as per your requirement.
CREATE FUNCTION ReplaceInvalidChar (@In_String VARCHAR(2000))
RETURNS VARCHAR(2000)
BEGIN
DECLARE @temp VARCHAR(2000) = @In_String;
DECLARE @List_Of_Invalid_Char VARCHAR (40) = '%[^0-9a-zA-Z_.#]%';
DECLARE @In_String_Len INT;
SELECT @In_String_Len = LEN(@In_String);
WHILE @In_String_Len > 0
BEGIN
SET @In_String_Len = @In_String_Len - 1;
IF (PATINDEX(@List_Of_Invalid_Char,@temp) > 0)
BEGIN
SELECT @temp = STUFF(@temp, PATINDEX(@List_Of_Invalid_Char,@temp),1,'');
END
ELSE
BEGIN
BREAK;
END
END
RETURN @temp
END
No comments:
Post a Comment