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. Concatenate several records into one

Concatenate several records into one

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpworkspace
3 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.
  • S Offline
    S Offline
    Steven J Jowett
    wrote on last edited by
    #1

    I have a table in MS SQL Server 2005 containing two fields :- TicketId and Narrative The data within the table may look as follows TicketId Narrative -------- --------- 302523 FARMS 302523 UNIT 8420 KGS 302524 VEHICLE BREAKDOWN 302525 REPAIR SCHEDULE Notice the first two records have the same ticket Id therefore are related. I need setup a view the returns 1 record per ticket, concatenating related narratives together e.g. TicketId Narrative -------- --------- 302523 FARMS UNIT 8420 KG 302524 VEHICLE BREAKDOWN 302525 REPAIR SCHEDULE Any help much appriciated Steve Jowett

    Z 1 Reply Last reply
    0
    • S Steven J Jowett

      I have a table in MS SQL Server 2005 containing two fields :- TicketId and Narrative The data within the table may look as follows TicketId Narrative -------- --------- 302523 FARMS 302523 UNIT 8420 KGS 302524 VEHICLE BREAKDOWN 302525 REPAIR SCHEDULE Notice the first two records have the same ticket Id therefore are related. I need setup a view the returns 1 record per ticket, concatenating related narratives together e.g. TicketId Narrative -------- --------- 302523 FARMS UNIT 8420 KG 302524 VEHICLE BREAKDOWN 302525 REPAIR SCHEDULE Any help much appriciated Steve Jowett

      Z Offline
      Z Offline
      zhengdong jin
      wrote on last edited by
      #2

      use function create function abc(@TicketId char(x)) returns char(xxx) as begin declare @ret char(xxxx) declare @tmp table ( .... ) insert into @tmp (...) select .... while ... begin ... set @ret = @ret + Narrative end return @ret end select TicketId,dbo.abc(ticketid) 'Narrative' from yourtable

      S 1 Reply Last reply
      0
      • Z zhengdong jin

        use function create function abc(@TicketId char(x)) returns char(xxx) as begin declare @ret char(xxxx) declare @tmp table ( .... ) insert into @tmp (...) select .... while ... begin ... set @ret = @ret + Narrative end return @ret end select TicketId,dbo.abc(ticketid) 'Narrative' from yourtable

        S Offline
        S Offline
        Steven J Jowett
        wrote on last edited by
        #3

        MY finalized function, if anyone is interested :- set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[ConcatNotes](@TicketId BigInt) RETURNS NVarChar(4000) AS BEGIN -- Declare the return variable here DECLARE @Narrative NvarChar(4000) DECLARE @Notes NVarChar(2000) DECLARE notes_cursor CURSOR FOR SELECT [Text] FROM Notes WHERE TicketId = @TicketId ORDER BY NoteId ASC OPEN notes_cursor FETCH NEXT FROM notes_cursor INTO @Notes SET @Narrative = '' WHILE @@FETCH_STATUS = 0 BEGIN IF LEN(@Narrative) > 0 BEGIN SET @Narrative = @Narrative + ' ¦ ' END SET @Narrative = @Narrative + @Notes FETCH NEXT FROM notes_cursor INTO @Notes END CLOSE notes_cursor DEALLOCATE notes_cursor -- Return the result of the function RETURN LTRIM(@Narrative) END Then in my SELECT statement SELECT TicketId, ConcatNotes(TicketId) AS Narrative FROM Tickets Thanks to Zhengdong Jin for pointing me in the right direction Steve Jowett

        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