Open In App

TRIM() Function in SQL Server

Improve
Improve
Like Article
Like
Save
Share
Report

TRIM() function :

This function in SQL Server is used to omit the space character or additional stated characters from the beginning or the ending of a specified string.

Features :

  • This function is used to omit the space character or some additional given characters from the beginning or the ending of a string stated.
  • This function accepts specific characters and string.
  • This function can by default omit the front and endmost spaces from a string given.

Syntax :

TRIM([characters FROM ]string)

Parameter :

This method accepts two parameters

  • characters FROM – Specific characters which are to be omitted.
  • string – Specified string from which the stated characters or spaces are omitted.

Returns :

It returns the specified string after omitting the space character or some additional stated characters from the front or the endmost part of it.

Example-1 :

Getting the specified string after omitting the beginning and ending space of it.

SELECT TRIM('     GFG     ');

Output :

GFG

Example-2 :

Using TRIM() function with a variable and getting the specified string after modification as output.

DECLARE @str VARCHAR(10);
SET @str = '   Geeks   ';
SELECT TRIM(@str);

Output :

Geeks

Example-3 :

Getting the specified string after omitting the specific characters and the beginning and ending space of it.

SELECT TRIM('@$ ' FROM '    @geeksforgeeks$    ');

Output :

geeksforgeeks

Here, the specified character is also omitted.

Example-4 :

Using TRIM() function with two variables and getting the specified string after modification as output.

DECLARE @str VARCHAR(10);
DEclare @char VARCHAR(10);
SET @str = '   &Geeks*  ';
SET @char = '&* ';
SELECT TRIM(@char FROM @str);

Output :

Geeks

Application :

This function is used to return the modified string after omitting the space character or additional stated characters from the beginning or the ending of a given string.


Last Updated : 18 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads