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

后端 未结 18 1783
情深已故
情深已故 2020-11-21 23:49

How could you remove all characters that are not alphabetic from a string?

What about non-alphanumeric?

Does this have to be a custom function or are there

18条回答
  •  生来不讨喜
    2020-11-22 00:31

    Though post is a bit old, I would like to say the following. Issue I had with above solution is that it does not filter out characters like ç, ë, ï, etc. I adapted a function as follows (I only used an 80 varchar string to save memory):

    create FUNCTION dbo.udf_Cleanchars (@InputString varchar(80)) 
    RETURNS varchar(80) 
    AS 
    
    BEGIN 
    declare @return varchar(80) , @length int , @counter int , @cur_char char(1) 
    SET @return = '' 
    SET @length = 0 
    SET @counter = 1 
    SET @length = LEN(@InputString) 
    IF @length > 0 
    BEGIN WHILE @counter <= @length 
    
    BEGIN SET @cur_char = SUBSTRING(@InputString, @counter, 1) IF ((ascii(@cur_char) in (32,44,46)) or (ascii(@cur_char) between 48 and 57) or (ascii(@cur_char) between 65 and 90) or (ascii(@cur_char) between 97 and 122))
    BEGIN SET @return = @return + @cur_char END 
    SET @counter = @counter + 1 
    END END 
    
    RETURN @return END
    

提交回复
热议问题