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

后端 未结 18 1326
情深已故
情深已故 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
    
    0 讨论(0)
  • 2020-11-22 00:21

    Try this function:

    Create Function [dbo].[RemoveNonAlphaCharacters](@Temp VarChar(1000))
    Returns VarChar(1000)
    AS
    Begin
    
        Declare @KeepValues as varchar(50)
        Set @KeepValues = '%[^a-z]%'
        While PatIndex(@KeepValues, @Temp) > 0
            Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')
    
        Return @Temp
    End
    

    Call it like this:

    Select dbo.RemoveNonAlphaCharacters('abc1234def5678ghi90jkl')
    

    Once you understand the code, you should see that it is relatively simple to change it to remove other characters, too. You could even make this dynamic enough to pass in your search pattern.

    Hope it helps.

    0 讨论(0)
  • 2020-11-22 00:21
    DECLARE @vchVAlue NVARCHAR(255) = 'SWP, Lettering Position 1: 4 Ω, 2: 8 Ω, 3: 16 Ω, 4:  , 5:  , 6:  , Voltage Selector, Solder, 6, Step switch, : w/o fuseholder '
    
    
    WHILE PATINDEX('%?%' , CAST(@vchVAlue AS VARCHAR(255))) > 0
      BEGIN
        SELECT @vchVAlue = STUFF(@vchVAlue,PATINDEX('%?%' , CAST(@vchVAlue AS VARCHAR(255))),1,' ')
      END 
    
    SELECT @vchVAlue
    
    0 讨论(0)
  • 2020-11-22 00:22

    From performance perspective I'd use Inline Function:

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE FUNCTION [dbo].[udf_RemoveNumericCharsFromString]
    (
    @List NVARCHAR(4000)
    )
    RETURNS TABLE 
    AS RETURN
    
        WITH GetNums AS (
           SELECT TOP(ISNULL(DATALENGTH(@List), 0))
            n = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
            FROM
              (VALUES (0),(0),(0),(0)) d (n),
              (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n),
              (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) f (n),
              (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) g (n)
                )
    
        SELECT StrOut = ''+
            (SELECT Chr
             FROM GetNums
                CROSS APPLY (SELECT SUBSTRING(@List , n,1)) X(Chr)
             WHERE Chr LIKE '%[^0-9]%' 
             ORDER BY N
             FOR XML PATH (''),TYPE).value('.','NVARCHAR(MAX)')
    
    
       /*How to Use
       SELECT StrOut FROM dbo.udf_RemoveNumericCharsFromString ('vv45--9gut')
       Result: vv--gut
       */
    
    0 讨论(0)
  • 2020-11-22 00:25

    Parameterized version of G Mastros' awesome answer:

    CREATE FUNCTION [dbo].[fn_StripCharacters]
    (
        @String NVARCHAR(MAX), 
        @MatchExpression VARCHAR(255)
    )
    RETURNS NVARCHAR(MAX)
    AS
    BEGIN
        SET @MatchExpression =  '%['+@MatchExpression+']%'
    
        WHILE PatIndex(@MatchExpression, @String) > 0
            SET @String = Stuff(@String, PatIndex(@MatchExpression, @String), 1, '')
    
        RETURN @String
    
    END
    

    Alphabetic only:

    SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', '^a-z')
    

    Numeric only:

    SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', '^0-9')
    

    Alphanumeric only:

    SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', '^a-z0-9')
    

    Non-alphanumeric:

    SELECT dbo.fn_StripCharacters('a1!s2@d3#f4$', 'a-z0-9')
    
    0 讨论(0)
  • 2020-11-22 00:25

    Here is another way to remove non-alphabetic characters using an iTVF. First, you need a pattern-based string splitter. Here is one taken from Dwain Camp's article:

    -- PatternSplitCM will split a string based on a pattern of the form 
    -- supported by LIKE and PATINDEX 
    -- 
    -- Created by: Chris Morris 12-Oct-2012 
    CREATE FUNCTION [dbo].[PatternSplitCM]
    (
           @List                VARCHAR(8000) = NULL
           ,@Pattern            VARCHAR(50)
    ) RETURNS TABLE WITH SCHEMABINDING 
    AS 
    
    RETURN
        WITH numbers AS (
            SELECT TOP(ISNULL(DATALENGTH(@List), 0))
                n = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
            FROM
            (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n),
            (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n),
            (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) f (n),
            (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) g (n)
        )
    
        SELECT
            ItemNumber = ROW_NUMBER() OVER(ORDER BY MIN(n)),
            Item = SUBSTRING(@List,MIN(n),1+MAX(n)-MIN(n)),
            [Matched]
        FROM (
            SELECT n, y.[Matched], Grouper = n - ROW_NUMBER() OVER(ORDER BY y.[Matched],n)
            FROM numbers
            CROSS APPLY (
                SELECT [Matched] = CASE WHEN SUBSTRING(@List,n,1) LIKE @Pattern THEN 1 ELSE 0 END
            ) y
        ) d
        GROUP BY [Matched], Grouper
    

    Now that you have a pattern-based splitter, you need to split the strings that match the pattern:

    [a-z]
    

    and then concatenate them back to get the desired result:

    SELECT *
    FROM tbl t
    CROSS APPLY(
        SELECT Item + ''
        FROM dbo.PatternSplitCM(t.str, '[a-z]')
        WHERE Matched = 1
        ORDER BY ItemNumber
        FOR XML PATH('')
    ) x (a)
    

    SAMPLE

    Result:

    | Id |              str |              a |
    |----|------------------|----------------|
    |  1 |    test“te d'abc |     testtedabc |
    |  2 |            anr¤a |           anra |
    |  3 |  gs-re-C“te d'ab |     gsreCtedab |
    |  4 |         M‚fe, DF |          MfeDF |
    |  5 |           R™temd |          Rtemd |
    |  6 |          ™jad”ji |          jadji |
    |  7 |      Cje y ret¢n |       Cjeyretn |
    |  8 |        J™kl™balu |        Jklbalu |
    |  9 |       le“ne-iokd |       leneiokd |
    | 10 |   liode-Pyr‚n‚ie |    liodePyrnie |
    | 11 |         V„s G”ta |          VsGta |
    | 12 |        Sƒo Paulo |        SoPaulo |
    | 13 |  vAstra gAtaland | vAstragAtaland |
    | 14 |  ¥uble / Bio-Bio |     ubleBioBio |
    | 15 | U“pl™n/ds VAsb-y |    UplndsVAsby |
    
    0 讨论(0)
提交回复
热议问题