代码之家  ›  专栏  ›  技术社区  ›  Robin

T-SQL微调(和其他非字母数字字符)

  •  7
  • Robin  · 技术社区  · 16 年前

    我们有一些有时会在末尾显示字符的输入数据。

    数据以varchar()的形式从源系统传入,我们尝试将这些字符强制转换为十进制失败。

    LTRIM和RTRIM不删除字符,因此我们必须执行以下操作:

    UPDATE myTable
    SET myColumn = replace(myColumn,char(160),'')
    WHERE charindex(char(160),myColumn) > 0
    

    这适用于 ,但对于任何非字母数字(或在本例中为数字)字符,是否有一种好的方法可以做到这一点?

    5 回复  |  直到 9 年前
        1
  •  9
  •   Espo    16 年前

    This page 具有如何删除非字母数字字符的示例:

    -- Put something like this into a user function:
    DECLARE @cString    VARCHAR(32)
    DECLARE @nPos    INTEGER
    SELECT  @cString = '90$%45623 *6%}~:@'
    SELECT  @nPos = PATINDEX('%[^0-9]%', @cString)
    
    WHILE @nPos > 0
    BEGIN
    SELECT @cString = STUFF(@cString, @nPos, 1, '')
    SELECT  @nPos = PATINDEX('%[^0-9]%', @cString)
    END
    
    SELECT @cString 
    
        2
  •  17
  •   PaulG    15 年前

    这将删除所有非字母数字字符

    CREATE FUNCTION [dbo].[fnRemoveBadCharacter]
    (
        @BadString nvarchar(20)
    )
    RETURNS nvarchar(20)
    AS
    BEGIN
    
                DECLARE @nPos INTEGER
                SELECT @nPos = PATINDEX('%[^a-zA-Z0-9_]%', @BadString)
    
                WHILE @nPos > 0
                BEGIN
                            SELECT @BadString = STUFF(@BadString, @nPos, 1, '')
                            SELECT @nPos = PATINDEX('%[^a-zA-Z0-9_]%', @BadString)
                END
    
                RETURN @BadString
    END
    

    使用如下功能:

    UPDATE TableToUpdate
    SET ColumnToUpdate = dbo.fnRemoveBadCharacter(ColumnToUpdate)
    WHERE whatever
    
        3
  •  1
  •   jason saldo    16 年前

    如何填充表?虽然可以在SQL中清除这一点,但更好的方法是将列类型更改为int,并在将数据加载到数据库(SSIS)之前清除数据。这是一个选择吗?

        4
  •  0
  •   BrandonNeiger    10 年前

    对于大型数据集,我最好使用这个检查ASCII值的函数。我添加了一些选项,只根据参数保留字母、数字或字母数字。

    --CleanType 1 - Remove all non alpanumeric
    --          2 - Remove only alpha
    --          3 - Remove only numeric
    CREATE FUNCTION [dbo].[fnCleanString] (
            @InputString    varchar(8000)
        ,   @CleanType      int 
        ,   @LeaveSpaces    bit 
    )   RETURNS varchar(8000)
    AS 
    BEGIN
    
        -- // Declare variables
        -- ===========================================================
        DECLARE @Length     int
            ,   @CurLength  int = 1
            ,   @ReturnString varchar(8000)=''
    
        SELECT @Length = len(@InputString)
    
        -- // Begin looping through each char checking ASCII value
        -- ===========================================================
        WHILE (@CurLength <= (@Length+1))
        BEGIN
            IF  (ASCII(SUBSTRING(@InputString,@CurLength,1)) between 48 and 57      AND @CleanType in (1,3) )
            or  (ASCII(SUBSTRING(@InputString,@CurLength,1))    between 65 and 90   AND @CleanType in (1,2) )
            or  (ASCII(SUBSTRING(@InputString,@CurLength,1))    between 97 and 122  AND @CleanType in (1,2) )
            or  (ASCII(SUBSTRING(@InputString,@CurLength,1))    = 32    AND @LeaveSpaces = 1 )
            BEGIN
                SET @ReturnString = @ReturnString + SUBSTRING(@InputString,@CurLength,1)
            END
            SET @CurLength = @CurLength + 1
        END
    
        RETURN  @ReturnString
    END
    
        5
  •  0
  •   Evaldas Buinauskas    9 年前

    如果手机可以以加号(+)开头,我将使用这样的功能

    CREATE FUNCTION [dbo].[Mobile_NoAlpha](@Mobile VARCHAR(1000)) 
    RETURNS VARCHAR(1000) 
    AS 
    BEGIN
        DECLARE @StartsWithPlus BIT = 0
    
        --check if the mobile starts with a plus(+)
        IF LEFT(@Mobile, 1) = '+'
        BEGIN
            SET @StartsWithPlus = 1
    
            --Take out the plus before using the regex to eliminate invalid characters
            SET @Mobile = RIGHT(@Mobile, LEN(@Mobile)-1) 
        END
    
        WHILE PatIndex('%[^0-9]%', @Mobile) > 0 
            SET @Mobile = Stuff(@Mobile, PatIndex('%[^0-9]%', @Mobile), 1, '')  
    
        IF @StartsWithPlus = 1
            SET @Mobile = '+' + @Mobile
        RETURN @Mobile 
    END