Solved

Concatenate ProductName and SubProductName separated by comma

Posted on 2011-02-17
6
540 Views
Last Modified: 2012-05-11
Hello Experts,

I am not completely new to sql but not familiar with everything as well. I am stumbled with one problem which I thought was easy :)

Anyways, here is the senario:
I have two tables named Products and SubProducts. Each product is related to some SubProducts. What I am trying to accomplish is return ProductName and SubProductName in following manner.

ProductNameOne|SubProductNameOne|SubProductNameTwo|SubProductNameThree

ProductNameTwo|SubProductNameOne|SubProductNameTwo|SubProductNameThree

ProductNameThree|SubProductNameOne|SubProductNameTwo|SubProductNameThree  

.......hope you get the idea. so concatenate ProductName and SubProductNames that are related to that ProductName.

So row one would contain concatenation of first ProductName and it's related SubProductNames
Rows two would contain concatenation of second ProductName and it's related SubProductNames....

Any ideas/suggestions are highly welcome.  

Thank you in advance for all you guys help :)
0
Comment
Question by:KoolAsIce
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 34919794
This will do it for you:
Replace anything in the <>

SELECT cast(p.<PARENTCOL>as varchar(50)) + ',' + el.values
FROM <PARENT TABLE> p
CROSS APPLY (SELECT cast(<CHILDCOL> as varchar(20)) + ',' AS [text()]
             FROM <CHILDTABLE>
             WHERE <PARENTCOL_ID = ChildTable_ParentCOL ID (FK>
             FOR XML PATH(''))el(values)


0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 34919799
forgot values was a keyword.  below will work.

SELECT cast(p.<PARENTCOL>as varchar(50)) + ',' + el.MYvalues
FROM <PARENT TABLE> p
CROSS APPLY (SELECT cast(<CHILDCOL> as varchar(20)) + ',' AS [text()]
             FROM <CHILDTABLE>
             WHERE <PARENTCOL_ID = ChildTable_ParentCOL ID (FK>
             FOR XML PATH(''))el(MYvalues)
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 34919830
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Closing Comment

by:KoolAsIce
ID: 34920081
You are genius! I was trying to do same thing using three temporary tables and almost 100 lines of code!

Thank you so much for your help!
0
 

Author Comment

by:KoolAsIce
ID: 34947447
Can someone tell me how to avoid '&' displaying as &amp;

In the attached code, the column SubCategoryName has a name containing '&' and result is displayed as '&amp;'

I assume it's because of the use of FOR XML PATH(' ').

All the help would be greatly appreciated on how to avoid that.  
0
 

Author Comment

by:KoolAsIce
ID: 34947790
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

770 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