2

After troubleshooting a sproc I realized the fields have an extra character, blank I assume or some special character not visible. For example, the city field contains the value 'Abbeville ' with a length (LEN([city]) of 10. I thought I could trim the white-space with the following:

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

That didn't work. I cast to varbinary but I don't know what to do from here. The column is defined as a nvarchar. Here's the cast value.

  0x4100620062006500760069006C006C006500A000

[Update] sample data

  City          City_Hex_Value
  Aaronsburg    0x4100610072006F006E0073006200750072006700A000
  Abbeville     0x4100620062006500760069006C006C006500A000

Thanks!

4
  • 1
    Len function returns the number of characters of the specified string expression, excluding trailing blanks. so length will be same before and after trimming Jan 13, 2015 at 5:04
  • Yes, so after the update, the length of the field should be 9 but it's still 10 @NoDisplayName
    – Will Lopez
    Jan 13, 2015 at 5:07
  • can show some sample data Jan 13, 2015 at 5:07
  • Can trim with another collation fix this?
    – jarlh
    Jan 13, 2015 at 7:55

2 Answers 2

14

Maybe the whitespace is not just a space:

Try this:

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

Explanation: Some white space are not really space (' '). Here are some of the whitespace:

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

What I did is remove the above whitespaces. You can replace it by space(' ') or CHAR(32), depends on how you want to handle them.

3
  • @WillLopez Glad I could help. Note that not all whitespace characters are included. Jan 13, 2015 at 5:15
  • @wewesthemenace Can you please demonstrate, I didn't get why you have used replace Jan 13, 2015 at 5:18
  • Thanks @wewesthemenace, noted. But the ones I needed were :-)
    – Will Lopez
    Jan 13, 2015 at 5:26
0

TRIM all SPACE's TAB's and ENTER's:

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 + '"'

As Function

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

Example

-- 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) + '"'

Result

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

Source: How to use a TRIM function in SQL Server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.