How to use a TRIM function in SQL Server

前端 未结 4 1914
情话喂你
情话喂你 2020-12-30 19:32

I cannot get this TRIM code to work

SELECT 
   dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR AS [EHP Code], 
   dbo.COL_TBL_VCOURSE.TNG_NA AS [Course Title], 
   LTR         


        
相关标签:
4条回答
  • 2020-12-30 20:07

    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    ]" |
    +-------+----------------+
    
    0 讨论(0)
  • 2020-12-30 20:08

    Example:

    DECLARE @Str NVARCHAR(MAX) = N'
                foo   bar
            Foo           Bar        
    
    '
    
    PRINT '[' + @Str + ']'
    
    DECLARE @StrPrv NVARCHAR(MAX) = N''
    
    WHILE ((@StrPrv <> @Str) AND (@Str IS NOT NULL)) BEGIN
        SET @StrPrv = @Str
    
        -- Beginning
        IF EXISTS (SELECT 1 WHERE @Str LIKE '[' + CHAR(13) + CHAR(10) + CHAR(9) + ']%')
            SET @Str = LTRIM(RIGHT(@Str, LEN(@Str) - 1))
    
        -- Ending
        IF EXISTS (SELECT 1 WHERE @Str LIKE '%[' + CHAR(13) + CHAR(10) + CHAR(9) + ']')
            SET @Str = RTRIM(LEFT(@Str, LEN(@Str) - 1))
    END
    
    PRINT '[' + @Str + ']'
    

    Result

    [
                foo   bar
            Foo           Bar        
    
    ]
    [foo   bar
            Foo           Bar]
    

    Using fnTrim

    Source: https://github.com/reduardo7/fnTrim

    SELECT dbo.fnTrim(colName)
    
    0 讨论(0)
  • 2020-12-30 20:10
    LTRIM(RTRIM(FCT_TYP_CD)) & ') AND (' & LTRIM(RTRIM(DEP_TYP_ID)) & ')'
    

    I think you're missing a ) on both of the trims. Some SQL versions support just TRIM which does both L and R trims...

    0 讨论(0)
  • 2020-12-30 20:21

    You are missing two closing parentheses...and I am not sure an ampersand works as a string concatenation operator. Try '+'

    SELECT dbo.COL_V_Cost_GEMS_Detail.TNG_SYS_NR AS [EHP Code], 
    dbo.COL_TBL_VCOURSE.TNG_NA AS [Course Title], 
    LTRIM(RTRIM(FCT_TYP_CD)) + ') AND (' + LTRIM(RTRIM(DEP_TYP_ID)) + ')' AS [Course Owner]
    
    0 讨论(0)
提交回复
热议问题