Question

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?

Was it helpful?

Solution 2

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), '')) + '!'

ماهر!

OTHER TIPS

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

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

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];

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top