Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SOLVED - How to concatenate column values of type text during SELECT...FROM...GROUP BY? [modified]

SOLVED - How to concatenate column values of type text during SELECT...FROM...GROUP BY? [modified]

Scheduled Pinned Locked Moved Database
databasetutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    liquid_
    wrote on last edited by
    #1

    I'd like to create a SQL statement which would do such thing: Having a table/query of these columns: number of type integer text of type text (string) I'd like execute such a SQL Statement: SELECT number,concatenate(text) FROM table GROUP BY number Resulting rows should contain concatenation of every value in column "text" for each number value. Is it possible at all?

    modified on Thursday, December 23, 2010 5:55 AM

    H 1 Reply Last reply
    0
    • L liquid_

      I'd like to create a SQL statement which would do such thing: Having a table/query of these columns: number of type integer text of type text (string) I'd like execute such a SQL Statement: SELECT number,concatenate(text) FROM table GROUP BY number Resulting rows should contain concatenation of every value in column "text" for each number value. Is it possible at all?

      modified on Thursday, December 23, 2010 5:55 AM

      H Offline
      H Offline
      Hiren solanki
      wrote on last edited by
      #2

      liquid_ wrote:

      Is it possible at all?

      Yes, That is possible. Suppose Consider AdventureWorks DB First create UDF fn_Join as Below,

      USE [AdventureWorks]
      GO
      SET ANSI_NULLS ON
      GO
      SET QUOTED_IDENTIFIER ON
      GO
      ALTER function [dbo].[fn_Join](@managerID int)
      returns varchar(max)
      AS
      BEGIN
      DEclare @ret varchar(max)
      SELECT @ret = SUBSTRING(
      (SELECT ',' + s.Title
      FROM HumanResources.Employee s
      WHERE ManagerID = @managerID
      FOR XML PATH('')),2,200000)
      RETURN(@ret)
      END

      That will give you a concatenated string of all the Title by giving ManagerID as a INPUT. Now write the query to fetch all the distinct ManagerID and it's corresponding concatenated Title

      select distinct ManagerID,dbo.fn_Join(ManagerID)
      from HumanResources.Employee
      WHERE ManagerID is not null

      That's will give you a desired result.

      Regards, Hiren.

      My Recent Article: - Way to know which control have raised a postback
      My Recent Tip/Trick: - Remove HTML Tag, get plain Text

      L 2 Replies Last reply
      0
      • H Hiren solanki

        liquid_ wrote:

        Is it possible at all?

        Yes, That is possible. Suppose Consider AdventureWorks DB First create UDF fn_Join as Below,

        USE [AdventureWorks]
        GO
        SET ANSI_NULLS ON
        GO
        SET QUOTED_IDENTIFIER ON
        GO
        ALTER function [dbo].[fn_Join](@managerID int)
        returns varchar(max)
        AS
        BEGIN
        DEclare @ret varchar(max)
        SELECT @ret = SUBSTRING(
        (SELECT ',' + s.Title
        FROM HumanResources.Employee s
        WHERE ManagerID = @managerID
        FOR XML PATH('')),2,200000)
        RETURN(@ret)
        END

        That will give you a concatenated string of all the Title by giving ManagerID as a INPUT. Now write the query to fetch all the distinct ManagerID and it's corresponding concatenated Title

        select distinct ManagerID,dbo.fn_Join(ManagerID)
        from HumanResources.Employee
        WHERE ManagerID is not null

        That's will give you a desired result.

        Regards, Hiren.

        My Recent Article: - Way to know which control have raised a postback
        My Recent Tip/Trick: - Remove HTML Tag, get plain Text

        L Offline
        L Offline
        liquid_
        wrote on last edited by
        #3

        Thanks but I'm afraid this does not work with MS Access. There is no straight way to create function or procedure or I don't know about it.

        H 1 Reply Last reply
        0
        • L liquid_

          Thanks but I'm afraid this does not work with MS Access. There is no straight way to create function or procedure or I don't know about it.

          H Offline
          H Offline
          Hiren solanki
          wrote on last edited by
          #4

          You could've mentioned MS ACCESS at a database.

          liquid_ wrote:

          There is no straight way to create function or procedure or I don't know about it.

          I also don't know as I've not worked much on ACCESS. :)

          Regards, Hiren.

          My Recent Article: - Way to know which control have raised a postback
          My Recent Tip/Trick: - Remove HTML Tag, get plain Text

          modified on Thursday, December 23, 2010 2:52 AM

          1 Reply Last reply
          0
          • H Hiren solanki

            liquid_ wrote:

            Is it possible at all?

            Yes, That is possible. Suppose Consider AdventureWorks DB First create UDF fn_Join as Below,

            USE [AdventureWorks]
            GO
            SET ANSI_NULLS ON
            GO
            SET QUOTED_IDENTIFIER ON
            GO
            ALTER function [dbo].[fn_Join](@managerID int)
            returns varchar(max)
            AS
            BEGIN
            DEclare @ret varchar(max)
            SELECT @ret = SUBSTRING(
            (SELECT ',' + s.Title
            FROM HumanResources.Employee s
            WHERE ManagerID = @managerID
            FOR XML PATH('')),2,200000)
            RETURN(@ret)
            END

            That will give you a concatenated string of all the Title by giving ManagerID as a INPUT. Now write the query to fetch all the distinct ManagerID and it's corresponding concatenated Title

            select distinct ManagerID,dbo.fn_Join(ManagerID)
            from HumanResources.Employee
            WHERE ManagerID is not null

            That's will give you a desired result.

            Regards, Hiren.

            My Recent Article: - Way to know which control have raised a postback
            My Recent Tip/Trick: - Remove HTML Tag, get plain Text

            L Offline
            L Offline
            liquid_
            wrote on last edited by
            #5

            With MS Access I had to use VB function which does similar thing. Anyway thanks for an idea.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups