SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[ConcatAcc]
(
	@Separator as nvarchar(1000),
	@Total as nvarchar(1000)=NULL,
	@Control as nvarchar(1000)=NULL,
	@Detail1 as nvarchar(1000)=NULL,
	@Detail2 as nvarchar(1000)=NULL
) RETURNS NVarCHAR(1000)
AS
BEGIN
	if ISNULL(@Total,'X')='X' or ISNULL(@Separator,'X')='X'
		return ''
	if ISNULL(@Control,'X')='X'
		return RTRIM(@Total)
	if ISNULL(@Detail1,'X')='X'
		return RTRIM(@Total)+@Separator+RTRIM(@Control)
	if ISNULL(@Detail2,'X')='X'
		return RTRIM(@Total)+@Separator+RTRIM(@Control)+@Separator+RTRIM(@Detail1)
	return RTRIM(@Total)+@Separator+RTRIM(@Control)+@Separator+RTRIM(@Detail1)+@Separator+RTRIM(@Detail2)
END
GO


