STRING_AGG workaround for SQL 2016

Michael "Captain Jack" Breen 21 Reputation points
2022-04-25T20:56:24.887+00:00

I have a vendor who wants data organized into a single string in a column. I know this violates coding standards for databases, so please spare me the lectures on how I'm in the wrong. I have to provide the data the way the vendor wants it and that's just the way it has to be.

Everything I've found recommends using a STUFF function, but the results I'm getting are not what I want. This is what I want:

Smith | Robert | email@keyman .com | EmployeeID | "B1, B3, B6"
Jones | Melanie | email@tiedtlaw email .com | EmployeeID | "B2"

where the B1, B3, B6, and B2 are the site assignments for each individual's respective sites.

What I am getting is this:
Smith | Robert | email@keyman .com | EmployeeID | "B1, B2, B3, B3, B4, B5, B6"
Jones | Melanie | email@tiedtlaw email .com | EmployeeID | "B1, B2, B3, B3, B4, B5, B6"

Here is the code I have that is the closest I've been able to get:

SELECT DISTINCT  
 sta.lastName as 'Last Name *',  
 sta.firstName as 'First Name *',  
 con.email as 'Email Address *',  
 sta.staffNumber as 'Staff ID *',  
 'Y' as 'Is Active *',  
 '"' + STUFF((SELECT DISTINCT', ' + css.[value]  /*The placement of the '"' puts double-quotes around the entire string in the column*/  
 FROM customSchool css  
 JOIN staffMember sta on sta.schoolID = css.schoolID  
 WHERE css.attributeID = 618 FOR XML PATH('')),1,2,'') + '"' [Building ID{s}*],  
 'Y' as 'Certificate Holder *'  
from staffMember sta  
JOIN contact con ON con.personID = sta.personID  
JOIN customSchool css on css.schoolID = sta.schoolID  
where sta.endDate IS NULL   
    AND sta.staffNumber is NOT NULL  
 AND con.email is NOT NULL  
 AND css.attributeID = 618  
order by sta.lastName  

Any functional help would be appreciated.

>UPDATE<<

I may not have been completely clear on what I'm trying to do and I apologize for that. I've attached a mock up below of what I want and what I'm getting:

196639-422project-2.png

I appreciate the help thus far. I feel I'm so close. Again, all functional help will be appreciated.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,747 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,553 questions
{count} votes

Accepted answer
  1. LiHong-MSFT 10,046 Reputation points
    2022-04-28T02:13:52.8+00:00

    Hi @Michael "Captain Jack" Breen
    According to the screenshot you post, you need to modify the subquery of STUFF function:

     SELECT DISTINCT  
      sta.lastName as 'Last Name *',  
      sta.firstName as 'First Name *',  
      con.email as 'Email Address *',  
      sta.staffNumber as 'Staff ID *',  
      'Y' as 'Is Active *',  
      '"' + STUFF((SELECT DISTINCT', ' + C.[value]  /*The placement of the '"' puts double-quotes around the entire string in the column*/  
                   FROM customSchool C JOIN staffMember S on S.schoolID = C.schoolID and S.StuffNumber=sta.StuffNumber  
                   WHERE C.attributeID = 618 FOR XML PATH('')),1,2,'') + '"' [Building ID{s}*],  
      'Y' as 'Certificate Holder *'  
     from staffMember sta  
     JOIN contact con ON con.personID = sta.personID  
     JOIN customSchool css on css.schoolID = sta.schoolID  
     where sta.endDate IS NULL   
           AND sta.staffNumber is NOT NULL  
           AND con.email is NOT NULL  
           AND css.attributeID = 618  
     order by sta.lastName  
    

    If it still doesn't work, please provide your table structure (CREATE TABLE …) and some sample data(INSERT INTO …). So that we’ll get a right direction and make some test.

    Best regards,
    LiHong

    1 person found this answer helpful.

6 additional answers

Sort by: Most helpful
  1. Michael "Captain Jack" Breen 21 Reputation points
    2022-04-27T16:19:56.583+00:00

    I give up!


  2. Michael "Captain Jack" Breen 21 Reputation points
    2022-04-28T16:15:36.993+00:00

    Thank you LiHong. That worked EXACTLY as I wanted. When I get more time, I'll compare mine to yours and see where I went wrong. That's going to be one I keep around for a long time.

    Thank you again.

    0 comments No comments