How to remove spaces from a string

To remove spaces from the left and teh right of a string uses sql LTRIM and RTRIM functions. The LTRIM function delete spaces from the left of the string. The RTRIM function delete spaces from the right of the string.

Syntax

RTRIM ( character_expression )

LTRIM ( character_expression )


Example

SELECT LTRIM(RTRIM('    Test    ')) AS Result;

Result
Test

Example

DECLARE @var_string varchar(100);
SET @var_string = '     Remove both left and the right spaces.     ';
SELECT LTRIM(RTRIM(@var_string));
GO

Result
Remove both left and the right spaces.