Open In App

TRIM() Function in SQL Server

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 :

Syntax :



TRIM([characters FROM ]string)

Parameter :

This method accepts two parameters

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.

Article Tags :
SQL