Solved

create a semicolon delimeted list

Posted on 2016-09-08
1
31 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 47

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 8 55
Is there any way to convert exponential value to number in sql server 5 38
sql server computed columns 11 29
TSQL - How to declare table name 26 29
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

786 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