Solved

Concatenate ProductName and SubProductName separated by comma

Posted on 2011-02-17
6
532 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now