Tags

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


NEW T-SQL FEATURES IN SQL SERVER 2017- II [ TRIM() ]

In this post we shall check out the new features Microsoft introduced in SQL Server 2017.

New feature – II | [ TRIM() ]

Removes the space character char(32) or other specified characters from the start or end of a string. Trust me this will going to be one of the most use functions going forward. People have been waiting for this function since very long.

Notes:-

1. So going forward we do not need to use the LTRIM(RTRIM()) functions. By default TRIM function removes the space character char(32) from both sides. This is equivalent to LTRIM(RTRIM(@string))
2. The function returns a string argument where the space character char(32) or other specified characters are removed from both sides.
3. The function returns NULL if input string is NULL.

Examples – Removes the space character from both sides of string

--

SELECT TRIM('   Pawan   ') OT

--

OUTPUT

--

OT
-----------
Pawan

(1 row affected)

--

Examples – Checking Lengths

--

SELECT '   Pawan   ' Input , DATALENGTH('   Pawan   ') InputLength , DATALENGTH(TRIM('   Pawan   ')) LengthAfterTrim 

--

OUTPUT

--

Input       InputLength LengthAfterTrim
----------- ----------- ---------------
   Pawan    11          5

(1 row affected)

--
Trim() - Remove LeadingAndTrailingSpaces

Trim() – Remove LeadingAndTrailingSpaces

Examples – Removes specified characters from both sides of string

--

SELECT TRIM( '.@ ' FROM  ' @   M .  Pawan    .') as OT

--

OUTPUT

--

OT
--------------------
M .  Pawan

(1 row affected)

--

Examples(2) – Removes specified characters from both sides of string

--

SELECT TRIM( '.@ ' FROM  '. @   M .  Pawan    .') as OT

--

OUTPUT

--

OT
---------------------
M .  Pawan

(1 row affected)


--

Refer below for more details-

1.https://docs.microsoft.com/en-us/sql/t-sql/functions/trim-transact-sql

Enjoy !!! Keep Learning

Pawan Khowal 

Http://MSBISkills.com