Using the STUFF command??

  • Hi

    The code below get the data I need but I want to comma separate the dbo.[common Items].Name field grouped by dbo.[Progress Note Common Selections].Parent

    Fairly sure I need the STUFF command just not sure how to set it up,

    SELECT dbo.[Progress Note Common Selections].Parent,

    CASE WHEN dbo.[common Items].Name = 'other' THEN 'Other: ' + dbo.[Progress Note Common Selections].OtherText ELSE dbo.[Common Items].Name

    END AS present

    FROM dbo.[Progress Note Common Selections] INNER JOIN

    dbo.[Common Items] ON dbo.[ Progress Note Common Selections].Item = dbo.[Common Items].ID INNER JOIN

    dbo.[Common Item Categories] ON dbo.[Common Items].Category = dbo.[Common Item Categories].ID

    ORDER BY dbo.[Progress Note Common Selections].Parent, present

    MY output looks like

    parent Present

    11111 Client

    11111 other:Parent

    I would like ...

    parent Present

    11111 Client, other:Parent

    Thanks in advance

    Joe

  • Joe

    The easiest way is to use a FOR XML clause in your SELECT statement. There are lots of examples out there - search for concatentate aggregate function. You'll probably need STUFF, but only to remove the trailing delimiter.

    John

  • Read the following article: http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    It explains what each part does. Be sure to understand how it works and ask any questions you might have. You could also read BOL to understand what the functions do.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thank you.. I am looking/looking up these now

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply