TSQL - Concatenate values into one string using a function

Hi Experts,

I want to be able to put all records in one column/String,  SQL Server 2008 R2


Right now I have this:
SELECT        CompanyName, Aff.[Address], Aff.Cntr3, Aff.Assn, Aff.Code2, Aff.Date1, Aff.Date2, Aff.Date6, 
				list.[Description], Memo1
FROM            tblNAsAffiliations Aff Join 
				fncUtlAddress() ADDR ON ADDR.Contact = Aff.Contact1 Join
				tblNAsMasterCodes MC ON MC.GroupType = Aff.Affiliation And MC.GroupCode = Aff.Code1 And MC.MasterCode = Aff.code2 Join
				tblNAsContacts C ON C.Contact = Aff.Contact1 Join tblNAsLists AS List On C.Contact = List.Contact And List.GroupType = 'RPQC' And List.GroupCode = '3ARPQC'
WHERE     	Aff.Affiliation = 'RPQC' And Aff.Code1 = 'RPQC' And Aff.Code3 = 'Approved' And Aff.Date2 > dbo.fncUtlGracePeriodApply(GetDate(), MC.Instructions)

Open in new window


But then I had help doing this:
SELECT        CompanyName, Aff.[Address], Aff.Cntr3, Aff.Assn, Aff.Code2, Aff.Date1, Aff.Date2, Aff.Date6, 
Memo1,
STUFF (
(
SELECT ',' + ISNULL([Description], '')
FROM tblNAsLists list 
where 
c.Contact = List.Contact And List.GroupType = 'RPQC' And List.GroupCode = '3ARPQC'
ORDER BY list.contact FOR XML PATH('')
)
, 1,1,'')  tblNADescs

FROM            tblNAsAffiliations Aff Join 
				fncUtlAddress() ADDR ON ADDR.Contact = Aff.Contact1 Join
				tblNAsMasterCodes MC ON MC.GroupType = Aff.Affiliation And MC.GroupCode = Aff.Code1 And MC.MasterCode = Aff.code2 Join
				tblNAsContacts C ON C.Contact = Aff.Contact1 
WHERE     	Aff.Affiliation = 'RPQC' And Aff.Code1 = 'RPQC' And Aff.Code3 = 'Approved' And Aff.Date2 > dbo.fncUtlGracePeriodApply(GetDate(), MC.Instructions)

Open in new window


That worked but I am told to use a function that has already been created to do the same:
ALTER FUNCTION [dbo].[fncUtlListConcatenateDelimiter]
-- =============================================
-- Author:		tjw
-- Create date: 9/30/2015
-- Description:	Duplicate of fncUtlListConcatenate, with the delimiter to be used passed  as a parameter
-- =============================================
(	
@GroupType nvarchar(10),
@GroupCode nvarchar(10),
@MasterCode nvarchar(10),
@DescriptionMode int,
@OrderBy nvarchar(10),
@Delimiter VARCHAR(255)
)
RETURNS TABLE 
AS

--DescriptionMode:
--0 MasterCode
--1 Description
--2 Description (include Other)
--3 MasterCode + Description
--4 MasterCode + Description (include Other)
--5 GroupCode:MasterCode

--OrderBy:
--(default) = mastercode
--QUANTITY = quantity
--DESCR = full description
--SORT = SortOrder value
--GROUP = groupcode, then mastercode

RETURN 
(
-- 9/23/2014:	Added a lot more rows, converted the underlying description to NVARCHAR(max) --tjw
SELECT L1.Contact,
		STUFF((

					SELECT @Delimiter +  
						 [Description]
					 FROM fncNAsListConcatentatePresort(@GroupType, @GroupCode, @MasterCode, @DescriptionMode) L2
					 WHERE L1.contact = L2.contact 

					 ORDER BY Contact,  CASE @OrderBy 
								WHEN 'QUANTITY' THEN CONVERT(nvarchar(10), Quantity)
								WHEN 'DESCR' THEN L2.[Description]
								WHEN 'SORT' THEN L2.SortOrder
								WHEN 'GROUP' THEN L2.MultiCode
								ELSE L2.MasterCode END , L2.[Description]
				FOR XML PATH(''), TYPE
				).value('.','varchar(max)')
			  ,1, LEN(@Delimiter), '') AS Lists

FROM  fncNAsListConcatentatePresort(@grouptype, @Groupcode, @mastercode,@descriptionMode) L1
GROUP BY L1.Contact
)

Open in new window


So it look like I need to start again on 8275121 but will need help on putting together.  This section will have to pass parms to the function:
Join tblNAsLists AS List On C.Contact = List.Contact And List.GroupType = 'RPQC' And List.GroupCode = '3ARPQC'

Open in new window


Please help and thanks
Amour22015Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
You are going to have to play around with the MasterCode, DescriptionMode, and OrderBy parameters to get what you want since they aren't well documented relative to your original query.

SELECT        CompanyName, Aff.[Address], Aff.Cntr3, Aff.Assn, Aff.Code2, Aff.Date1, Aff.Date2, Aff.Date6, 
Memo1, dbo.fncUtlListConcatenateDelimiter(List.GroupType, List.GroupCode, <mastercode>, <descriptionMode>, <OrderBy>, ',') AS tblNADescs
FROM            tblNAsAffiliations Aff Join 
				fncUtlAddress() ADDR ON ADDR.Contact = Aff.Contact1 Join
				tblNAsMasterCodes MC ON MC.GroupType = Aff.Affiliation And MC.GroupCode = Aff.Code1 And MC.MasterCode = Aff.code2 Join
				tblNAsContacts C ON C.Contact = Aff.Contact1
				INNER JOIN tblNAsLists AS List On C.Contact = List.Contact
WHERE     	Aff.Affiliation = 'RPQC' And Aff.Code1 = 'RPQC' And Aff.Code3 = 'Approved' And Aff.Date2 > dbo.fncUtlGracePeriodApply(GetDate(), MC.Instructions)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amour22015Author Commented:
Great thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.