Merge the String Group by Column

  • Dear All,

    I have a two column like DESIGNATIONS,NAME

    Values

    DESIGNATIONS NAME

    MD SARA

    MD RAJ

    Manager Joe

    Manager Freddy

    Here I have to merge the Name like below wise

    Output

    DESIGNATIONS NAME

    MD SARA RAJ

    Manager Joe Freddy

    Please help me to pull the data from table...

  • Try this:

    SELECT p1.DESIGNATIONS,

    STUFF( ( SELECT ',' + NAME

    FROM TableNAME p2

    WHERE p2.DESIGNATIONS = p1.DESIGNATIONS

    ORDER BY NAME

    FOR XML PATH('')

    ) , 1,1, SPACE(0)) AS Concat_Values

    FROM TableNAME p1

    GROUP BY p1.DESIGNATIONS;

  • Thank u so much , I just made small alternations that is DISTINCT for removing duplications & LTRIM & RTRIM for removing white spaces..

    Its amazing working fine,,,

  • One thing to be aware of when doing this on a large scale is that you have no suitable column to ORDER BY on. Ordering by name will sometimes put the first name ahead of the last name, and sometimes put the last name ahead of the first name.

    Bob Smith

    Smith Tom

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

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

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