T-SQL修剪(和其他非字母数字字符)

时间:2008-09-09 16:39:17

标签: sql sql-server

我们有一些输入数据有时会出现在&nbsp个字符的末尾。

数据从源系统进入varchar(),我们尝试转换为十进制失败b / c这些字符。

Ltrim和Rtrim不会删除这些字符,因此我们不得不这样做:

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

这适用于& nbsp,但对于任何非字母数字(或在本例中为数字)字符,是否有一种好方法可以执行此操作?

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:9)

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 :(得分:1)

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

答案 3 :(得分: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

答案 4 :(得分:0)

如果手机可以以Plus(+)开头,我会使用像这样的功能

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