SQL SERVER - FOR XML PATH assistance

Experts,

I have a table that looks like this:


REC_ID      USER_ID      REGION      DISTRICT      CLUSTER
1      BATEST50      000      875      072
1      BATEST50      000      876      073
2            NULL      NULL      NULL
3      BATEST50      000      875      072
3      BATEST50      000      876      073


REC_ID 2 has a NULL or blank USER_ID and NULL Region,District,Cluster columns.


This code:

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



Produces this output result which groups the Region,District,Cluster for each user together, separating the RDCs with a "!" and and attempts to separate each Rec_ID with an "&":

RESULT_ID
000~875~072!000~876~073&000~875~072!000~876~073

------------------

I need to produce this output result showing 2 ampersands together which mean a NULL record:

000~851~442!000~875~072&&000~851~442!000~875~072


Also, IF the final REC_ID is the null, I need the ampersand to appear at the END of the string which will mean that there is a null REC_ID at the end.

Can someone help me with the CODE or a solution for this?


Thanks!
LVL 17
MIKESoftware Solutions ConsultantAsked:
Who is Participating?
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.

chaauCommented:
Try coalesce. This will produce the result you want:
SELECT  '&' + COALESCE(REGION + '~' + DISTRICT + '~' + CLUSTER, '') AS RDC,

Open in new window

Try coalesce. This will produce the result I think is more accurate, ie &~~&:
SELECT  '&' + COALESCE(REGION, '') + '~' + COALESCE(DISTRICT, '') + '~' + COALESCE(CLUSTER, '') AS RDC,

Open in new window

0

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:
Is there any way to remove the 2 curved dashes when the USER_ID is NULL?

Here is what the result is:

000~875~072!000~876~073&~~&000~875~072!000~876~073

Here is what I'm seeking:

000~875~072!000~876~073&&000~875~072!000~876~073

Thanks
0
chaauCommented:
Have you tried my first solution:
SELECT  '&' + COALESCE(REGION + '~' + DISTRICT + '~' + CLUSTER, '') AS RDC,

Open in new window

It should remove the curly dashes
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
CRXIuser2005, do you still need help with this question?
0
MIKESoftware Solutions ConsultantAuthor Commented:
I only need the curly dashes removed IF there is a NULL for the USER_ID:

Here is what the result is:

000~875~072!000~876~073&~~&000~875~072!000~876~073

Here is what I'm seeking:

000~875~072!000~876~073&&000~875~072!000~876~073

And if the NULL appears at the end of the I need a single "&" to appear at the end showing that there is another field at the end..but it is NULL....
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Use the replace function:
SELECT  REPLACE('&' + REGION + '~' + DISTRICT + '~' + CLUSTER AS RDC, '~~', '~')

Open in new window

0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.