CREATE FUNCTION dbo.trim( @val nvarchar(max) )
RETURNS nvarchar(max)
BEGIN
DECLARE @tab nchar(1)
, @lf nchar(1)
, @cr nchar(1)
, @crlf nchar(2)
SET @tab = char(9)
SET @lf = char(10)
SET @cr = char(13)
SET @crlf = char(13) + char(10)
RETURN ( replace(replace(replace(replace(ltrim(rtrim(@val)), @tab, ''), @crlf, ''), @lf, ''), @cr, '' ) )
END --trim()