Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

When using REPLACE I'm getting an extra comma

$
0
0

My current SQL query using REPLACE yields the results I want but adds a "," in between spaces in the field for some reason.

SELECTDISTINCT[Contract Number],

 REPLACE((SELECT[User Name]AS 'data()'

          FROM TABLEAT

                WHERET.[Contract Number]=TABLEA.[Contract Number]ANDT.[User Role]='Peer'FORXMLPATH('')),' ',', ')AS[Peer]

 ,REPLACE((SELECT[User Name]AS 'data()'

          FROM TABLEAT

                WHERET.[Contract Number]= TABLEA.[Contract Number]ANDT.[User Role]='Admin'FORXMLPATH('')),' ',', ')AS[Admin]

FROMTABLEA

This takes...

Contract Number       User                   Role

XX-XX-123               Mike Schmitt       Peer

XX-XX-123               Jake Jills             Peer

XX-XX-123               Adam Snow        Admin

XX-YY-456               Lisa Sneed          Admin

XX-YY-456               Bryan Wilson       Admin

XX-YY-456               Labron James      Peer

and makes it into this...

Contract Number      Admin                                 Peer

XX-XX-123              Adam, Snow                         Mike, Schmitt, Jake, Jills

XX-YY-456              Lisa, Sneed, Bryan, Wilson       Labron, James

Why are these extra '," being added?


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>