create function dbo.fnConcat ( @a varchar(max) ,@delim varchar(max) ,@b varchar(max) ) returns varchar(max) as begin declare @result varchar(max) ,@aLen int ,@bLen int select @a = RTRIM(ltrim(coalesce(@a,''))) ,@aLen = LEN(@a) ,@b = RTRIM(ltrim(coalesce(@b,''))) ,@bLen = LEN(@b) select @result = case when @aLen > 0 and @bLen > 0 then @a + @delim + @b when @aLen > 0 then @a when @bLen > 0 then @b else '' end return @result end