-- trimming via LTrim, RTrim SELECT LTrim(RTrim(AddressLine1)) AddressLine1 , LTrim(RTrim(AddressLine2)) AddressLine2 , LTrim(RTrim(City)) City , LTrim(RTrim(PostalCode)) PostalCode FROM Person.Address
This might not look much though imagine you have to deal with 30-50 text attributes, that the code is not written in a readable format (e.g. the way is stored in database), that some attributes require further processing (e.g. removal of special characters, splitting, concatenating).
Often developers preferred encapsulating the call to the two functions within a user-defined function:
-- Trim user-defiend function CREATE FUNCTION dbo.Trim( @string nvarchar(max)) RETURNS nvarchar(max) BEGIN RETURN LTrim(RTrim(@string)) END
With it the code is somehow simplified, but not by much and includes the costs of calling a user-defined function:
-- trimming via dbo.Trim SELECT dbo.Trim(AddressLine1) AddressLine1 , dbo.Trim(AddressLine2) AddressLine2 , dbo.Trim(City) City , dbo.Trim(PostalCode) PostalCode FROM Person.Address
In SQL Server 2017 was introduced the Trim function which not only replaces the combined use of LTrim and RTrim functions, but also replaces other specified characters (including CR, LF, Tab) from the start or end of a string.
By default the function removes the space from both sides of a string:
-- trimming via Trim SELECT Trim(AddressLine1) AddressLine1 , Trim(AddressLine2) AddressLine2 , Trim(City) City , Trim(PostalCode) PostalCode FROM Person.Address
When a set of characters is provided the function removes the specified characters:
SELECT Trim ('#' FROM '# 843984') Example1 , Trim ('[]' FROM '[843984]') Example2 , Trim ('+' FROM '+49127298000') Example3 , Trim ('+-' FROM '+ 49-12729-8000 ') + ';' Example4 , Trim ('+ ' FROM '+ 49-12729-8000 ') + ';' Example5 , ASCII(Left(Trim (char(13) FROM char(13) + '49127298000'), 1)) Example6
Output:
Example1 Example2 Example3 Example4 Example5 Example6
-------- -------- ------------ ----------------- ----------------- -----------
843984 843984 49127298000 49-12729-8000 ; 49-12729-8000; 52
As can be seen when is needed to remove other characters together with the space then is needed to include the space in the list of characters.
Notes:
The dbo.Trim function can be created in SQL Server 2017 environments as well.
The collation of the database will affect the behavior of Trim function, therefore the results might look different when a case sensitive collection is used.
Happy coding!