Results 1 to 6 of 6

Thread: SQL to concatenate

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    SQL to concatenate

    SQL Server 2000

    I have two tables(tblParent and tblChild) as follows :
    HTML Code:
    tblParent          tblChild
    PID                 CID     PID       Name
                 
    1           |         89     1         Fred
    2           |         90     1         Jane
                          91     2         Rod
    The result I want from my query is as follow:
    Code:
    PID         Name
    1            Fred, Jane
    2            Rod

    Is this possible and if so can somone show me the sql required?

    Thanks In Advance all

    Parksie

  2. #2
    Lively Member
    Join Date
    Dec 2008
    Posts
    103

    Re: SQL to concatenate

    Here you go.

    Code:
    SELECT P.PID, C.Name
    from tblParent P INNER JOIN tblChild C ON P.PID = C.PID
    This will give the following output
    Code:
    PID         Name
    1            Fred, 
    1            Jane
    2            Rod
    Please add to my rep if this helps. thanks.

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: SQL to concatenate

    Thats not what I wanted.
    I need to, in this case return 2 rows. I want the fields concatenated

    Parksie

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL to concatenate

    try searching for "concatenation" in posts by slzamany ... I know I've seen him post that before... it's one of those tricks, when I see it, I think "how can that work" ... but it does. Unfortunately it isn't something I use, so it never sticks.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: SQL to concatenate

    To handle multiple child records without having to create an unnecessarily complex join, create a stored procedure that generates the CSV (stored procedure retrieves records, iterates through them, builds CSV and returns CSV to caller).

    You should be mindful of data sizing issues though, e.g. report item widths, since you don't know how long string returned by procedure will be in the future.

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL to concatenate

    You can do it with a function
    Code:
    CREATE FUNCTION fnConcatenateNames(@ID INT)
    RETURNS VARCHAR(1000) AS
    BEGIN
      DECLARE @names VARCHAR(1000)
      SELECT @names = COALESCE(@names + ', ', '') + COALESCE(Name,'')
      FROM b 
      WHERE PID = @ID
      return @names
    END
    And use it like
    Code:
    SELECT ID
         , dbo.fnConcatenateNames(ID) AS Names
    FROM a
    GROUP BY ID
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width