SQL 服务器 RTRIM(LTRIM([City])) 未去除空格

SQL Server RTRIM(LTRIM([City])) not stripping whitespace

在对存储过程进行故障排除后,我意识到这些字段有一个额外的字符,我认为是空白或一些不可见的特殊字符。例如,城市字段包含值 'Abbeville ',长度 (LEN([city]) 为 10。我想我可以 trim white-space 具有以下内容:

  UPDATE US_City_State
  SET [City] = RTRIM(LTRIM([City]))

那没用。我投射到 varbinary 但我不知道从这里做什么。该列定义为 nvarchar。这是施法值。

  0x4100620062006500760069006C006C006500A000

[更新]样本数据

  City          City_Hex_Value
  Aaronsburg    0x4100610072006F006E0073006200750072006700A000
  Abbeville     0x4100620062006500760069006C006C006500A000

谢谢!

也许白色space不只是一个space:

试试这个:

  UPDATE US_City_State
  SET [City] = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE([City], CHAR(10), ''), CHAR(13), ''), CHAR(9), ''), CHAR(160), '')))

解释: 有些白space其实不是space('')。下面是一些小白space:

CHAR(9)     =   Horizontal Tab
CHAR(10)    =   Line Feed
CHAR(13)    =   Carriage Return
CHAR(160)   =   Non-Breaking Space

我所做的是删除上面的白色spaces。您可以将其替换为 space(' ') 或 CHAR(32),具体取决于您希望如何处理它们。

TRIM 所有 SPACETABENTER 的:

DECLARE @Str VARCHAR(MAX) = '      
          [         Foo    ]       
          '

DECLARE @NewStr VARCHAR(MAX) = ''
DECLARE @WhiteChars VARCHAR(4) =
      CHAR(13) + CHAR(10) -- ENTER
    + CHAR(9) -- TAB
    + ' ' -- SPACE

;WITH Split(Chr, Pos) AS (
    SELECT
          SUBSTRING(@Str, 1, 1) AS Chr
        , 1 AS Pos
    UNION ALL
    SELECT
          SUBSTRING(@Str, Pos, 1) AS Chr
        , Pos + 1 AS Pos
    FROM Split
    WHERE Pos <= LEN(@Str)
)
SELECT @NewStr = @NewStr + Chr
FROM Split
WHERE
    Pos >= (
        SELECT MIN(Pos)
        FROM Split
        WHERE CHARINDEX(Chr, @WhiteChars) = 0
    )
    AND Pos <= (
        SELECT MAX(Pos)
        FROM Split
        WHERE CHARINDEX(Chr, @WhiteChars) = 0
    )

SELECT '"' + @NewStr + '"'

作为函数

CREATE FUNCTION StrTrim(@Str VARCHAR(MAX)) RETURNS VARCHAR(MAX) BEGIN
    DECLARE @NewStr VARCHAR(MAX) = NULL

    IF (@Str IS NOT NULL) BEGIN
        SET @NewStr = ''

        DECLARE @WhiteChars VARCHAR(4) =
              CHAR(13) + CHAR(10) -- ENTER
            + CHAR(9) -- TAB
            + ' ' -- SPACE

        IF (@Str LIKE ('%[' + @WhiteChars + ']%')) BEGIN

            ;WITH Split(Chr, Pos) AS (
                SELECT
                      SUBSTRING(@Str, 1, 1) AS Chr
                    , 1 AS Pos
                UNION ALL
                SELECT
                      SUBSTRING(@Str, Pos, 1) AS Chr
                    , Pos + 1 AS Pos
                FROM Split
                WHERE Pos <= LEN(@Str)
            )
            SELECT @NewStr = @NewStr + Chr
            FROM Split
            WHERE
                Pos >= (
                    SELECT MIN(Pos)
                    FROM Split
                    WHERE CHARINDEX(Chr, @WhiteChars) = 0
                )
                AND Pos <= (
                    SELECT MAX(Pos)
                    FROM Split
                    WHERE CHARINDEX(Chr, @WhiteChars) = 0
                )
        END
    END

    RETURN @NewStr
END

例子

-- Test
DECLARE @Str VARCHAR(MAX) = '      
          [         Foo    ]       
              '

SELECT 'Str', '"' + dbo.StrTrim(@Str) + '"'
UNION SELECT 'EMPTY', '"' + dbo.StrTrim('') + '"'
UNION SELECT 'EMTPY', '"' + dbo.StrTrim('      ') + '"'
UNION SELECT 'NULL', '"' + dbo.StrTrim(NULL) + '"'

结果

+-------+----------------+
| Test  | Result         |
+-------+----------------+
| EMPTY | ""             |
| EMTPY | ""             |
| NULL  | NULL           |
| Str   | "[   Foo    ]" |
+-------+----------------+

来源: How to use a TRIM function in SQL Server