Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Join 2 tables & get a comma seperated list of names

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-06-20 : 09:40:51
Neera writes "Hi,

I have 2 tables.

create table Table1(
TId int
)

create table Table2(
Name varchar(300),
TString varchar(3000)
)
Table1 has:

TId
300
234
456
789
999
9
3
8

Table2 has:

Name TString
A, 300,234,9,8
B, 456,234,3,8
C 300,789,999
D 999,234,9,8

The result I shoud get as:
TId Name
300 A,C
234 A,B,D
456 B
789 C
999 C,D
9 A,D
3 B
8 A,B,D
Can anyone help me please..

Thanks,
Neera"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-20 : 09:55:45
Try

Select T1.id, T2.name from table1 T1 inner join table2 T2
on ','+T2.name+',' like '%,'+cast(t1.id as varchar(10))+',%'

You need to read about Normalisation
http://www.datamodel.org/NormalizationRules.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -