Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a TRIM function in SQL Server

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],     LTRIM(RTRIM(FCT_TYP_CD)& ') AND (' & LTRIM(RTRIM(DEP_TYP_ID) & ')' AS [Course Owner] 
like image 687
user1958651 Avatar asked Jan 21 '13 21:01

user1958651


People also ask

How does the trim function work in SQL?

SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.

How do I trim a column in SQL?

SQL Server does not support for Trim() function. But you can use LTRIM() to remove leading spaces and RTRIM() to remove trailing spaces. can use it as LTRIM(RTRIM(ColumnName)) to remove both.

What is the equivalent of trim in SQL Server?

By default, the TRIM function removes the space character from both the start and the end of the string. This behavior is equivalent to LTRIM(RTRIM(@string)) .

How do you trim both sides in SQL?

TRIM() function The SQL TRIM() removes leading and trailing characters(or both) from a character string.


2 Answers

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] 
like image 112
maelstrom Avatar answered Sep 30 '22 23:09

maelstrom


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    ]" | +-------+----------------+ 
like image 30
Eduardo Cuomo Avatar answered Sep 30 '22 22:09

Eduardo Cuomo