Solved

Join 2 tables & get a Comma seperated list of names

Posted on 2006-06-19
6
244 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

688 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