Author |
Topic |
cykophysh
Starting Member
24 Posts |
Posted - 2007-04-12 : 09:05:11
|
Hi guys,I need to retrieve a comma separated values from the database.i.e I have a linking table that contains a Branch To Location association, by associating a BranchID to a LocalityID.My Query Looks like thisSelect dbo.LOCALITY.LOCALITY from dbo.LOCALITY inner join dbo.BranchLocality on dbo.BranchLocality.LOCALITY_ID = dbo.LOCALITY.ID where dbo.BranchLocality.BRANCH_ID = 34 This returns a Table.i.e New YorkAlaskaCaliforniaWyomingUtahWhat I would Like to achieve is that I return it as one field with a a Comma Separating the valuesi.e "New York, Alaska, California, Wyoming, Utah" etcKind Regards,Gary<A href="http://www.threenineconsulting.com" target="_blank" >My Website</a> || <a href="http://threenineconsulting.com/forum/blogs/cykophysh/default.aspx" targer="_blank">My Blog</a> |
|
Kristen
Test
22859 Posts |
|
cykophysh
Starting Member
24 Posts |
Posted - 2007-04-12 : 09:14:52
|
Sorry For wasting your timeI found the answer with the help of this article[url]http://www.sqlteam.com/item.asp?ItemID=2368[/url]Kind Regards,Gary<A href="http://www.threenineconsulting.com" target="_blank" >My Website</a> || <a href="http://threenineconsulting.com/forum/blogs/cykophysh/default.aspx" targer="_blank">My Blog</a> |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-12 : 10:03:59
|
Try thisSELECT DISTINCT b.BRANCH_ID, STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + l.LOCALITY FROM dbo.LOCALITY AS l WHERE b.LOCALITY_ID = l.ID ORDER BY ',' + l.LOCALITY FOR XML PATH('')), 1, 1, '') AS LocalitiesFROM dbo.BranchLocality AS bORDER BY b.BRANCH_ID Peter LarssonHelsingborg, Sweden |
 |
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2007-04-12 : 10:37:01
|
quote: Originally posted by cykophysh Hi guys,I need to retrieve a comma separated values from the database.i.e I have a linking table that contains a Branch To Location association, by associating a BranchID to a LocalityID.My Query Looks like thisSelect dbo.LOCALITY.LOCALITY from dbo.LOCALITY inner join dbo.BranchLocality on dbo.BranchLocality.LOCALITY_ID = dbo.LOCALITY.ID where dbo.BranchLocality.BRANCH_ID = 34 This returns a Table.i.e New YorkAlaskaCaliforniaWyomingUtahWhat I would Like to achieve is that I return it as one field with a a Comma Separating the valuesi.e "New York, Alaska, California, Wyoming, Utah" etcKind Regards,Gary<A href="http://www.threenineconsulting.com" target="_blank" >My Website</a> || <a href="http://threenineconsulting.com/forum/blogs/cykophysh/default.aspx" targer="_blank">My Blog</a>
try this with case stmt ...DECLARE @STR VARCHAR(1000)SELECT @STR = (CASE WHEN @STR IS NULL THEN <ur field> ELSE @STR + ', ' + <ur field> END)FROM <ur table>SELECT @STR AS CSV_STRINGMahesh |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-12 : 10:50:30
|
However I am interested to know where you want to show the concatenated data?MadhivananFailing to plan is Planning to fail |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-04-12 : 17:04:10
|
"I found the answer with the help of this articlehttp://www.sqlteam.com/item.asp?ItemID=2368"Given that this is a SQL 2005 forum you may find that that article is not "safe" for SQL 2005.I suspect that you need the code that Peso posted, above.Kristen |
 |
|
mabbj747
Starting Member
2 Posts |
Posted - 2010-05-07 : 05:18:50
|
DECLARE @LocalityList varchar(100)SELECT @LocalityList = COALESCE(@LocalityList + ', ', '') + CAST(dbo.LOCALITY.LOCALITYAS varchar(5))FROM dbo.LOCALITY INNER JOIN dbo.BranchLocality ON dbo.BranchLocality.LOCALITY_ID = dbo.LOCALITY.IDWHERE dbo.BranchLocality.BRANCH_ID = 34SELECT @LocalityList |
 |
|
|