Question

I have a table that consists of notes people have made against a job number. This data was taken off an old system which stored each line of text as a new table row. What I want to do is to join each line together to form a single string for each job number.

So the following table

JobNo | Line | Notes  
C1234 |  1   | blah blah blah.......  
C1234 |  2   | blah blah blah.....  
C1234 |  3   | blah blah blah.  
C1235 |  1   | blah blah blah blah blah blah.  
C1236 |  1   | blah blah blah....  

Would become

JobNo | Notes  
C1234 | blah blah blah....... blah blah blah..... blah blah blah.  
C1235 | blah blah blah blah blah blah.  
C1236 | blah blah blah....  
Was it helpful?

Solution

If it's TSQL try using STUFF with a FOR XML PATH :-

(Note this will only work with sql server 2005 onwards)

SELECT
   JobNo,
   STUFF(
     (SELECT ' ' + Notes
      FROM MyTable
      WHERE JobNo = a.JobNo
      FOR XML PATH (''))
      , 1, 1, '')  AS URLList
FROM MyTable AS a
GROUP BY JobNo

See this SQL FIDDLE working example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top