How to Put Vertical Column Value into the same row

  • Hi,

    I have a table in the following format.

    Title                      Name

    Manager                John Doe

    Manager                 Mr. X

    Manager                 Ms. Y

     

    I'd like is to have the resultset in the following format for my Reporting.

    Title                       Name

    Manager                 John Doe;Mr. X; Ms. Y

     

    Any help would be really appreciated. Thanks a lot.

     

     

  • See if this helps: http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=156835

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Something like this:

     

    IF EXISTS( SELECT * FROM sysobjects WHERE name = 'T1' )

     DROP TABLE T1

    GO

    CREATE TABLE T1(  C1  NCHAR(1)  )

    SET NOCOUNT ON

    INSERT T1 VALUES( 'A' )

    INSERT T1 VALUES( 'B' )

    INSERT T1 VALUES( 'C' )

    DECLARE @str VARCHAR (50) -- attention with size, can trim results

     SET @str = ' '

     SELECT @str = @str + C1 + '; ' -- any separator here, like '|', ' ', ','

       FROM  T1

     SELECT  @str

    DROP TABLE T1

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

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