Home » sql server ltrim

sql server ltrim

Using TRIM Function in SQL Server: – Sql server ltrim for Left Trim in sql server 2008 .  Sql server rtrim for Right Trim for sql trim characters from Right..  Trim in sql server 2014 has not changed and below article still shows how to trim column in sql server

 

Using TRIM Functions are two types which are
1.LTrim
2.RTrim

LTrim: The string without the leading spaces can be obtained by using LTrim.

Please make sure you use L or R as if you you only Trim you will see below erro

‘trim’ is not a recognized built-in function name.

Syntax: Select LTRIM ( character_expression or Column_name ), “column_name2″,….”column_nameZ” FROM “table_name”

Example:
DECLARE @string_to_trim varchar(60)
SET @string_to_trim = ‘ Five spaces are at the beginning of this string.’
SELECT ‘Here is the string without the leading spaces: ‘ +
LTRIM(@string_to_trim)
GO

Result:
————————————————————————
Here is the string without the leading spaces: Five spaces are at the beginning of this string.

RTrim: The string without the spaces at the end of string can be obtained by using RTrim.

Syntax: Select RTRIM ( character_expression or Column_name ), “column_name2″,….”column_nameZ” FROM “table_name”

Example:
DECLARE @string_to_trim varchar(60);
SET @string_to_trim = ‘Four spaces are after the period in this sentence. ‘;
SELECT RTRIM(@string_to_trim) + ‘ Next string.’;
GO

Result:
————————————————————————-

Four spaces are after the period in this sentence. Next string.