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

后端 未结 18 1330
情深已故
情深已故 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:19

    --First create one function

    CREATE FUNCTION [dbo].[GetNumericonly]
    (@strAlphaNumeric VARCHAR(256))
    RETURNS VARCHAR(256)
    AS
    BEGIN
         DECLARE @intAlpha INT
         SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)
    BEGIN
         WHILE @intAlpha > 0
       BEGIN
              SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
              SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric )
       END
    END
    RETURN ISNULL(@strAlphaNumeric,0)
    END
    

    Now call this function like

    select [dbo].[GetNumericonly]('Abhi12shek23jaiswal')
    

    Its result like

    1223
    

提交回复
热议问题