TSQL - Concatenate values into one string

Hi Experts,

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


Right now I have this:
Join ( Select Contact, (@Concat = @Concat + LEFT(',', LEN(@Concat)) + ISNULL([Description], ''))  From tblNAsLists) AS List On C.Contact = List.Contact 

Open in new window


But this is not correct.

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.

Kyle AbrahamsSenior .Net DeveloperCommented:
select 
c.*, STUFF(
(
SELECT ',' + ISNULL([Description], '')
FROM tblNAsLists list where 
c.Contact = List.Contact  ORDER BY list.contact FOR XML PATH('')), 1,1,'') )
) tblNADescs
from <tbl> c

Open in new window

0
Amour22015Author Commented:
Ok so 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


How do I make all fit into what I currently have?

Please help and thanks...
0
Kyle AbrahamsSenior .Net DeveloperCommented:
try the following:

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

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Amour22015Author Commented:
ok great but one little problem I need to put a space between each

Right now it is doing:
Cat,Dog,Mouse

I need:
Cat, Dog, Mouse

Please help
0
Kyle AbrahamsSenior .Net DeveloperCommented:
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, 2,'')  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

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 for your help, on the last posting you sent, putting the space worked, but I am also using Access reports and I noticed that the #name error came up and only thing I can think about is that it reached the max for that field in Access.  Thanks again...
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.