Often we need to trim a string to remove the leading and trailing whitespace characters.
SQL server does not provide a TRIM() function.
But it provides LTRIM() and RTRIM() functions.
LTRIM() is essentially a function that removes the spaces from the left of a string, in other words the leading spaces.
Similarly RTRIM() function removes the spaces from the right of a string, in other words the trailing spaces.
But none of these functions removes other whitespace characters like tab, newline, carriage return and line feed.
In this post we will create a user defined function (UDF) that trims the spaces from head and tail and also remove the other whitespace characters from the string.
The signature of LTRIM() and RTRIM() functions are as bellow:
LTRIM (character_expression )
RTRIM (character_expression )
Here character_expression is the string that we want to be trimmed.
declare @str as nvarchar(max)
set @str = ' A string with leading and trailing spaces '
select 'Prev_String'+LTRIM(@str)+'Next_String'
Returned result is: Prev_StringA string with leading and trailing spaces Next_String
select 'Prev_String'+RTRIM(@str)+'Next_String'
Returned result is:
Prev_String A string with leading and trailing spacesNext_String
Now if we want to trim both the leading and trailing spaces, then we can simply use both the functions.
select 'Prev_String'+LTRIM(RTRIM(@str))+'Next_String'
Returned result is:
Prev_StringA string with leading and trailing spacesNext_String
We can also create an UDF with this so that we don't need to always use both LTRIM and RTRIM.
CREATE FUNCTION DBO.TRIM(@STR NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
BEGIN
RETURN LTRIM(RTRIM(@STR))
END
We can use
SELECT TRIM(@str)
Now this is only the first part of this post. We have only removed spaces.
What about other white space characters?
Often we need to load data from csv or fixed length files and we have some invisible characters at the end of our string.
LTRIM() or RTRIM() is not going to help us in those cases.
These other whitespace characters include tab(\t), carriage return (cr), line feed(lf) or newline (\n).
To remove these characters we can replace these characters with a blank character.
SELECT REPLACE(@str, '\t', ")
But this is not going to work and \t is not the actual character for tab.
We will need to use the unicode character values of these whitespace characters.
SQL server provides CHAR() function that takes an integer value and returns the unicode character for the corresponding integer code.
The CHAR function has the following signature:
CHAR (integer_expression )
The unicode value of the whitespace characters are:
|
Control character
|
Value
|
| Tab |
char(9)
|
| Line feed |
char(10)
|
| Carriage return |
char(13)
|
| Space |
Char(32)
|
So to remove tabs from a string we can use
REPLACE(@str, CHAR(9), ")
Similarly we can remove the Line Feed and Carriage return.
We assume that tab, line feed and carriage returns are not acceptable characters anywhere in a string.
So we can finally create our whitespace removal function as:
CREATE FUNCTION DBO.TRIM(@STR NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
BEGIN
declare @TAB nvarchar(2), @LF nvarchar(2), @CR nvarchar(2),@NL varchar(2)
set @TAB = char(9)
set @LF = char(10)
set @CR = char(13)
set @NL = char(13)+char(10)
return replace(replace(replace(replace(LTRIM(RTRIM(@STR))
, @TAB, "), @NL, "), @LF, "),@CR, ")
END