Solved

convert XML path to semicolon delimeted list by joining tables

Posted on 2016-09-13
1
37 Views
Last Modified: 2016-09-20
I have the following query

select s.UserRoleId,
STUFF(( select eh.ChildUserRoleID from stgERMHierarchy eh
where eh.UserRoleID = s.UserRoleId
for XML Path('')),1,0,'') as concatenatedText
from StgERMUser s
inner join HubUser h on h.Login = s.NetworkID
inner join stgERMHierarchy eh on eh.UserRoleID = s.UserRoleId
group by s.UserRoleId
order by s.UserRoleId


The resultset that I get is something like this :

userRoleId                    concatenatedText
344321762               <ChildUserRoleID>326</ChildUserRoleID><ChildUserRoleID>343</ChildUserRoleID>
396433216              <ChildUserRoleID>105500128</ChildUserRoleID><ChildUserRoleID>105608599</ChildUserRoleID><ChildUserRoleID>343</ChildUserRoleID>

I want to take the concatenatedText column and read each childuserroleid and get actual login name for that particular userrole and show it in semicolon delimeted.

so it should be something like this

userRoleId                    concatenatedText                                                                                                                                                       login
344321762               <ChildUserRoleID>326</ChildUserRoleID><ChildUserRoleID>343</ChildUserRoleID>                                     ppp;sss  
396433216              <ChildUserRoleID>105500128</ChildUserRoleID>                                                                                                    ttt;yyy;zzz
                                   <ChildUserRoleID>105608599</ChildUserRoleID>          <ChildUserRoleID>343</ChildUserRoleID>

Thanks
0
Comment
Question by:PratikShah111
1 Comment
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
I cannot test it as I dont have test data.
I assume you have a login table and you can join to that table to get the name.
try something like this.
select s.UserRoleId,
STUFF(( select eh.ChildUserRoleID from stgERMHierarchy eh
where eh.UserRoleID = s.UserRoleId
for XML Path('')),1,0,'') as concatenatedText,
STUFF(( select lt.name from loginTable lt
where eh.ChildUserRoleID = lt.UserRoleID
for XML Path('')),1,0,'') as loginNames
from StgERMUser s
inner join HubUser h on h.Login = s.NetworkID
inner join stgERMHierarchy eh on eh.UserRoleID = s.UserRoleId
group by s.UserRoleId
order by s.UserRoleId

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

771 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