2

I am constantly selecting columns from a table after trimming them like the following:

SELECT TOP 1 RTRIM(LTRIM([UN_DataIN])) FROM [Names]

This is returning the name Fadi

SELECT TOP 1 RTRIM(LTRIM([UN_DataIN])), LEN(RTRIM(LTRIM([UN_DataIN]))) FROM [Names]

when I select the length of the trimmed column, I get back 10.

Which means RTRIM and LTRIM are not doing their jobs.

Is there an alternative to them?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
HelpASisterOut
  • 3,085
  • 16
  • 45
  • 89

6 Answers6

3

This is may work for you as my problem too.. ^-^

select rtrim(ltrim(replace(replace(replace(colname,char(9),' '),char(10),' '),char(13),' ')))
from yourtable

source : http://www.sqlservercentral.com/Forums/Topic288843-8-1.aspx

0

I would imagine you have some new lines in your field text. RTRIM and LTRIM do not handle those very well.

wergeld
  • 14,332
  • 8
  • 51
  • 81
  • I think this is the case. Any suggestion on how to trim those? – HelpASisterOut Jan 30 '14 at 12:58
  • Depending on how much data you have and if it is occurring in all instances you can manually fix (not fun). Or you can convert to ASCII, and replace the ASCII code for new line `CHAR(13)` or `CHAR(10)`. – wergeld Jan 30 '14 at 12:59
0

Create Function that provide your desired output like this:

CREATE FUNCTION dbo.TRIM(@string VARCHAR(MAX))
RETURNS VARCHAR(MAX)
BEGIN
   RETURN LTRIM(RTRIM(Regex_Replace(@string,'\n','')));
END
GO

Here, we are using regex_replace to remove if any New Lines detected, after that it will apply RTRIM and LTRIM

Now you can Call this TRIM function in query:

SELECT TOP 1 dbo.TRIM([UN_DataIN]) FROM [Names];
124
  • 2,757
  • 26
  • 37
0

UN_DataIN == 0x45062706470631062920292029202920292029202000

So presuming Arabic your string ends with Unicode paragraph separators U+2029 and then a single whitespace all of which you need to remove;

select rtrim(replace(UN_DataIN, nchar(0x2029), '')) + '!'

ماهر!

Alex K.
  • 171,639
  • 30
  • 264
  • 288
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

Community
  • 1
  • 1
Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
0

My workaround if you have one space at the end:

update tableName set PayeeName = PayeeName + '_Serengeti' where right(PayeeName,1) = ' '
update tableName set PayeeName = Replace(PayeeName,' _Serengeti','') where PayeeName like '%_Serengeti%'

If you have many spaces, yo may need to execute the statements until the spaces are all removed.

Kemboi
  • 1
  • 1
  • 2