SQL SERVER - FOR XML PATH

EXPERTS,

I have a stored procedure that I need to return data in a concatenated string format like this:

Reg~Dist~Clus!Reg~Dist~Clus!Reg~Dist~Clus&Reg~Dist~Clus!

In this example I have 3 UserIDS, the first 2 have only ONE Region,District,Cluster combination but the last one has 2 combinations.

I need to use FOR XML PATH to product this display.

RDC
000~002~351!000~037~309!000~003~124&000~003~148!


The "&" sign needs to be used to separate Reg,Dist,Clus combos within the same USER_ID.

This is what my source table looks like:

USER_ID      REGION      DISTRICT CLUSTER
253020      000      002      351
253254      000      037      309
253339      000      003      124
253339      000      003      148

This is my current "FOR XML PATH" SQL:

--FINAL DISPLAY
; WITH CTE_RDC_DISPLAY AS
(
SELECT
(SELECT REGION+'~'+DISTRICT+'~'+CLUSTER+'!'
FROM #TEMP
  --GROUP BY USER_ID,REGION,DISTRICT,CLUSTER
        FOR XML PATH('')) RDC

)
SELECT RDC
FROM CTE_RDC_DISPLAY


This SQL currently produces THIS:

RDC
000~002~351!000~037~309!000~003~124!000~003~148!


I need to have the XML modified to return THIS:

RDC
000~002~351!000~037~309!000~003~124&000~003~148!


The reason is that the last 2 RDC combinations belong to the SAME USER_ID.

Thanks.
LVL 17
MIKESoftware Solutions ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
I think this may work. The ROW_NUMBER tells whether to use the "!" or "&".

However, notice that the FOR XML will escape the "&" into "&".

If you don't like that, just wrap the whole SUBSTRING...'!' with a REPLACE(<query>, '&amp;', '&').

;WITH CTE_RDC_DISPLAY (RDC, uRank) AS
(
SELECT  '!' + REGION + '-' + DISTRICT + '-' + CLUSTER,
        ROW_NUMBER() OVER (PARTITION BY USER_ID ORDER BY REGION) AS uRank
FROM    @Temp
)
SELECT  SUBSTRING((SELECT  CASE
                               WHEN uRank > 1 THEN REPLACE(RDC, '!', '&')
                               ELSE RDC
                           END
                   FROM    CTE_RDC_DISPLAY
                   FOR XML PATH('')), 2, 9999) + '!'

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MIKESoftware Solutions ConsultantAuthor Commented:
NICE,...you got me very close....

here is the result I'm getting:
000-002-351!000-037-309!000-003-124&amp;000-003-148!

Can you tell me WHY I'm seeing '&amp' in the data instead of just '&'....?

Do you know how to  fix this?

THANKS!!!
MIKESoftware Solutions ConsultantAuthor Commented:
sorry...didn't read entire comment...

LOOKS LIKE ITS GOING TO WORK..!!
MIKESoftware Solutions ConsultantAuthor Commented:
EXCELLENT !!!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.