Sunday, 29 December 2019

Trim Function in sql server 2017


The SQL trim function removes the space character char (32) or other specified characters from the start or end of a string. It is the combination of RTRIM and LTRIM Function.
·         LTRIM () function is used to truncate all leading blanks or white-spaces from the left side of the string.
·         RTRIM () function is used to truncate all trailing blanks or white-spaces from the right side of the string.
Select  TRIM('  Bagesh Kumar Singh    ')
Select LTRIM('  Bagesh Kumar Singh    ')
Select RTRIM('  Bagesh Kumar Singh    ')
     
       

Trim can be used to remove specific characters from both sides of a String.
Syntax:
TRIM ([characters FROM] string)
See the example
SELECT TRIM('~`!@#$%^&*?<>?|\' FROM 'Bagesh Kumar Singh')  As Full_Name
SELECT TRIM('~`!@#$%^&*?<>?|\' FROM '!@#$%^ Bagesh Kumar Singh') As Full_Name
SELECT TRIM('~`!@#$%^&*?<>?|\' FROM '!@#$%^ Bagesh Kumar Singh&*?<>?|\')  As Full_Name
SELECT TRIM('~`!@#$%^&*?<>?|\' FROM 'Bagesh ~`!@#$%^&*?<>?|\ Kumar Singh') As Full_Name
       
     

We can use characters also see example




No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts