Solved

create a semicolon delimeted list

Posted on 2016-09-08
1
30 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 46

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

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

863 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

18 Experts available now in Live!

Get 1:1 Help Now