[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Join 2 tables & get a Comma seperated list of names

Posted on 2006-06-19
6
Medium Priority
?
247 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
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

830 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