Friday, 15 May 2015

Remove extra spaces at the end of column in sql server




Similarly we can remove the Line Feed and Carriage return. We assume that tab,
line feed and carriage returns are not acceptable characters anywhere in a string.
So we can finally create our whitespace removal function as:


CREATE FUNCTION DBO.TRIM(@STR NVARCHAR(MAX))

RETURNS NVARCHAR(MAX)

BEGIN

declare @TAB nvarchar(2), @LF nvarchar(2), @CR nvarchar(2),@NL varchar(2)

set @TAB = char(9)

set @LF = char(10)

set @CR = char(13)

set @NL = char(13)+char(10)

return replace(replace(replace(replace(LTRIM(RTRIM(@STR)), @TAB, “), @NL, “), @LF, “), @CR, “)

END

No comments:

Post a Comment