Thursday, 20 July 2017

How to remove all non-alphabetic characters from string in SQL Server?

Create a function to solve the given problem:

CREATE FUNCTION RemoveNonAlphaCharacters
(@StringName VARCHAR(1000))
RETURNS VARCHAR(1000)
AS
BEGIN
     DECLARE @KeepValues as Varchar(50)
     SET @KeepValues='%[^a-z]%'

     WHILE PATINDEX(@KeepValues,@StringName)>0
     SET @StringName= STUFF(@StringName,PATINDEX(@KeepValues,@StringName),1,'')

     RETURN @StringName
END



Now, Call the Function as below

select DBO.RemoveNonAlphaCharacters('fefwsAA1456sdfsf')

output : fefwsAAsdfsf

No comments:

Post a Comment