【问题标题】:T-SQL trim &nbsp (and other non-alphanumeric characters)T-SQL 修剪 &nbsp(和其他非字母数字字符)
【发布时间】:2010-09-08 07:33:34
【问题描述】:

我们有一些输入数据有时会以 &nbsp 字符结尾。

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

Ltrim 和 Rtrim 不会删除字符,所以我们不得不这样做:

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

这适用于 &nbsp,但是对于任何非字母数字(或者在这种情况下为数字)字符,有没有一种好的方法可以做到这一点?

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

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

    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
    

    【讨论】:

      【解决方案2】:

      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 
      

      【讨论】:

      • 这将删除除 0-9(数字)之外的所有内容。 .... 所以它并不是真的在做 alpha numberic :P ..
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        对于大型数据集,我对这个检查 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】:

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

          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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-04
            • 1970-01-01
            • 2012-06-21
            • 2018-01-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多