0

I have a query that produces the following results

NAME         WeekPattern

John Smith   
John Smith   //
John Smith   OO
Jack Jones   
Jack Jones   O
Jack Jones   //

Is there a way I can concatenate the WeekPattern column so the results will show like this:

NAME         WeekPattern

John Smith     // OO
Jack Jones     O //

I have tried using FOR XML PATH but couldn't get it to work, here is the query for my attempt:

SELECT 
p.p_surname,
p.p_forenames,
LEFT(sr.sr_weekpattern , LEN(sr.sr_weekpattern )-1) AS Weekpattern
FROM 
unitesnapshot.dbo.capd_studentregister sr
INNER JOIN unitesnapshot.dbo.capd_person p ON p.p_id = sr.sr_student
CROSS APPLY
(
    SELECT sr.sr_weekpattern + ' '
    FROM unitesnapshot.dbo.capd_studentregister sr1
    INNER JOIN unitesnapshot.dbo.capd_person p1 ON p1.p_id = sr1.sr_student

    WHERE p.p_id = p1.p_id
    FOR XML PATH('')
) pre_trimmed (Weekpattern)
GROUP BY p.p_surname, p.p_forenames, sr.sr_weekpattern;

I get this error when running this query:

Invalid length parameter passed to the LEFT or SUBSTRING function.

Any help would be much appreciated

2 Answers 2

2
SELECT
     Name,
     STUFF(
         (SELECT ' ' + WeekPattern
          FROM TableName
          WHERE NAME = a.NAME AND WeekPattern IS NOT NULL
          FOR XML PATH (''))
          , 1, 1, '')  AS WeekPatternList
FROM TableName AS a
GROUP BY Name
Sign up to request clarification or add additional context in comments.

1 Comment

can you give sample records along with your question?
1

Based on your existing query, you are not referencing the correct column in the LEFT, you should be using:

SELECT 
  p.p_surname,
  p.p_forenames,
  LEFT(pre_trimmed.Weekpattern , LEN(pre_trimmed.Weekpattern)-1) AS Weekpattern
FROM unitesnapshot.dbo.capd_studentregister sr
INNER JOIN unitesnapshot.dbo.capd_person p 
  ON p.p_id = sr.sr_student
CROSS APPLY
(
  SELECT sr1.sr_weekpattern + ' '
  FROM unitesnapshot.dbo.capd_studentregister sr1
  INNER JOIN unitesnapshot.dbo.capd_person p1 
    ON p1.p_id = sr1.sr_student
  WHERE p.p_id = p1.p_id
  FOR XML PATH('')
) pre_trimmed (Weekpattern)

5 Comments

This answer works but only for half a second then it gives the error:Invalid length parameter passed to the LEFT or SUBSTRING function.
@Will does the subquery work on its own if you remove the WHERE p.p_id = p1.p_id?
@Will the subquery doesn't have the left so how does that throw an error? Does the subquery inside of the cross apply work on its own?
sorry, when WHERE p.p_id = p1.p_id the same error is thrown. When the subquery is run by itself it wont run because p.p_id and sr.sr_weekpattern are out of scope
@Will you have to remove that where clause and inside of that subquery it should be select sr1.sr_weekpattern + ' ' as I changed in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.