Solved

create a semicolon delimeted list

Posted on 2016-09-08
1
26 Views
Last Modified: 2016-09-08
I am trying to write a select statement  which will return a semicolon delimeted list. Here is what I want.

Currently my select statement is returning me the following resultset

userroleid          childuserroleid
   4                               4
   4                             10
   4                               7
   8                             12
   8                              13
   8                              15
   11                             44
    12                            89

I want the select statement to return following

userroleid          childuserroleid
   4                               4;10;7
   8                             12;13;15
   11                             44
    12                            89


Thanks
0
Comment
Question by:PratikShah111
1 Comment
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41789735
Use STUFF function together with FOR XML PATH output:
SELECT  userroleid,
          STUFF((SELECT ',' + childuserroleid
                    FROM TableName T2
                    WHERE T2.userroleid = T1.userroleid
                    FOR XML PATH('') ), 1, 1, '' )
            AS ConcatenatedText
FROM  TableName T1

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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.
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.

708 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

12 Experts available now in Live!

Get 1:1 Help Now