Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Join 2 tables & get a Comma seperated list of names

Posted on 2006-06-19
6
Medium Priority
?
246 Views
Last Modified: 2011-10-03
Hi,

I have 2 tables like this:

Table1:
TID int

Table2:
NAME varchar,
TSTRING varchar

Table1  Data:

TID
101
200
309
408
987
9
8


Table2 Data:
NAME        TSTRING
A              101,200,408,987
B              9,8,101,200
C              987,408,309

TSTRING id a list of TID's.
The result I should get USING SQLserver 2000 as

TID           Name
101            A,B
200            A,B
309            C
408            A,C
987            A,C
9               B
8               B

Can anyone know how to do this?

Thanks,
Neera
0
Comment
Question by:neerak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 16939033
go back and revisit your design...

once you store your data in properly normalised tables...
then the client side display results you desire will be trivial to achieve..

and ps convince me this isn't homework please...
0
 

Author Comment

by:neerak
ID: 16939236
Here is the clear description:
I have the  SQL Server 2000 database with a table "ZONES" and a table of "TERMS".  Each entry in the ZONES table contains a ZONE Id 'ZID'  and Name of the ZONE "ZNAME"  and Zone String "ZSTRING", which is a comma seperated list of term id' & it can vary not fixed.Each TERM has a TERM Id "TID", Term Name "TNAME", etc..

How can I create a query or cursor that gives the comma seperated list of Zone names "ZNAMES" for the terms with TIDS?

ZONES table:

ZID      ZNAME       ZSTRING
 ---    ------        ----------
1        TX           101,200,408,987
2        VA           9,8,101,200
3        MA          987,408,309

TERMS table:
TID                 TNAME
----                 --------
101                 HOU
200                 ADH
309                 INH
408                 HALL
987                 SYHI
9                     KLI
8                    TPU


The Result I should get as:
TID          ZONENAMES
----         ---------------
101             TX,VA    
200             TX,VA  
309              MA
408             TX,MA
987             TX,MA    
9                 VA
8                 VA

I am working on some application, I am confused here, If you could help that would be great.

Thanks,
neera

0
 
LVL 10

Expert Comment

by:StephenCairns
ID: 16939560
Where does the "TERMS table: " come into this?
The Result I should get as:
TID          ZONENAMES
----         ---------------
101             TX,VA    
200             TX,VA  
309              MA
408             TX,MA
987             TX,MA    
9                 VA
8                 VA
 
is simply
ZID      ZNAME       ZSTRING
 ---    ------        ----------
1        TX           101,200,408,987
2        VA           9,8,101,200
3        MA          987,408,309

the other way round

0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:neerak
ID: 16939605
"ZSTRING " from ZONES table consists of list of TID's from TERMS table.
I tried to convert the ZSTRING from ZONE table into a temp table & joined the this temp table with TERMS table TID.
0
 

Author Comment

by:neerak
ID: 16939634
In the the Result, I have to get, the TID is from the TERMS table
TID          ZONENAMES
----         ---------------
101             TX,VA    
200             TX,VA  
309              MA
408             TX,MA
987             TX,MA    
9                 VA
8                 VA
0
 
LVL 17

Accepted Solution

by:
HuyBD earned 1500 total points
ID: 16939814
This is not good way but good result :))

Create Function dbo.getlist (@id as int)
Returns varchar(1000)
As

BEGIN

Declare @reval varchar(1000)

Select @reval = IsNull(@reval + ',', '') + ZNAME
From ZONES
where (','+rtrim(ZSTRING)+',') like ('%,'+rtrim(cast(@id as char(10)))+',%')
Return @reval
END

And then:
select TID, dbo.getlist(TID) as ZONENAMES from TERMS
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question