15.9.08

Ltrim / Rtrim in SQL Server

Trim function is a very important function in database part.
Sometimes, our front end application will populate some spaces with out actual data.
While validating the data ( "X" is not equal to " X") we will get improper data. To avoid this, SQL Server provides an option of trimming those white spaces.

For trimming the white space before the data, LTRIM function is used.
For trimming the white space after the data, RTRIM function is used.
For trimming both sides, we will use both function like LTRIM(RTRIM(ColumnName)

DECLARE @VenkatTable TABLE (id int, val varchar(10))
INSERT INTO @VenkatTable VALUES(1,' Venkat')
INSERT INTO @VenkatTable VALUES(1,' Ve nkat')
INSERT INTO @VenkatTable VALUES(1,' Venkat ')
INSERT INTO @VenkatTable VALUES(1,'Venkat ')
SELECT * FROM @VenkatTable
SELECT LTRIM(RTRIM(val)) as val FROM @VenkatTable



Happy Learning!!!
Regards,
Venkatesan Prabu .J

1 comment: