7

我们有一些输入数据,有时会在末尾出现   字符。

数据来自源系统作为 varchar() 并且我们尝试转换为十进制失败 b/c 这些字符。

Ltrim 和 Rtrim 不删除字符,所以我们被迫做类似的事情:

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

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

4

5 回答 5

17

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

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
于 2009-05-18T18:02:01.200 回答
9

此页面包含如何删除非字母数字字符的示例:

-- 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 
于 2008-09-09T16:46:56.960 回答
1

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

于 2008-09-09T17:03:35.907 回答
0

对于大型数据集,我对这个检查 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
于 2015-03-10T17:47:13.983 回答
0

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

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
于 2016-03-09T12:40:09.893 回答