Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7963417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:36:11+00:00 2026-06-04T05:36:11+00:00

I have a database which is including two tables, Labs and LabUsers How do

  • 0

I have a database which is including two tables, Labs and LabUsers
How do I join a column from LabUsers into a cell in Labs.

To be specific, I would like to have the user names of a lab to be concatenated and separated with a bullet ( • is Alt+0149), and to have these usernames sorted alphabetically left to right.

Here is an example.

The Labs table looks like this:

LabID  LabName     LabStudents
-----  ----------  -----------
1      North       NULL
2      North East  NULL
3      South West  NULL

and the LabUsers looks like this:

LabUserID   LabUserName   LabID
---------   -----------   -----    
1           Diana         1
2           Paul          2
3           Paula         2
4           Romeo         1
5           Julia         1
6           Rose          2
7           Diana         2

I would like to get this outcome in the Labs table:

LabID  LabName     LabUsers
-----  ----------  ---------------------
1      North       Diana•Julia•Romeo
2      North East  Diana•Paul•Paula•Rose
3      South West  NULL

Here is the script to create the tables:

USE [tempdb];
GO
CREATE TABLE [dbo].[LabUsers]
(
    [LabUserID] [int] PRIMARY KEY CLUSTERED,
    [LabUserName] [nvarchar](50) NOT NULL,
    [LabID] [int] NOT NULL
);
GO
INSERT [dbo].[LabUsers] SELECT 1, N'Diana', 1;
INSERT [dbo].[LabUsers] SELECT 2, N'Paul',  2;
INSERT [dbo].[LabUsers] SELECT 3, N'Paula', 2;
INSERT [dbo].[LabUsers] SELECT 4, N'Romeo', 1;
INSERT [dbo].[LabUsers] SELECT 5, N'Julia', 1;
INSERT [dbo].[LabUsers] SELECT 6, N'Rose',  2;
INSERT [dbo].[LabUsers] SELECT 7, N'Diana', 2;

CREATE TABLE [dbo].[Labs]
(
    [LabID] [int] PRIMARY KEY CLUSTERED,
    [LabName] [nvarchar](50) NOT NULL,
    [LabUsers] [nvarchar](max) NULL
);
GO
INSERT [dbo].[Labs] SELECT 1, N'North',      NULL;
INSERT [dbo].[Labs] SELECT 2, N'North East', NULL;
INSERT [dbo].[Labs] SELECT 3, N'South West', NULL;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:36:12+00:00Added an answer on June 4, 2026 at 5:36 am
    SELECT l.LabID, l.LabName, LabUsers = STUFF((SELECT N'•' + lu.LabUserName
      FROM dbo.LabUsers AS lu 
      WHERE lu.LabID = l.LabID
      ORDER BY lu.LabUserName
      FOR XML PATH(''), 
      TYPE).value(N'./text()[1]', N'nvarchar(max)'), 1, 1, N'')
    FROM dbo.Labs AS l;
    

    I see absolutely no reason to store this in the table, since you can always generate the code at runtime when you run a query. If you store it in the table, then you have to update it every single time you change any row in the table.

    However, if I can’t convince you not to do this (it really is bad to store redundant data like this), you can try this way:

    ;WITH x AS
    (
        SELECT l.LabID, l.LabName, x = STUFF((SELECT N'•' + lu.LabUserName
          FROM dbo.LabUsers AS lu 
          WHERE lu.LabID = l.LabID
          ORDER BY lu.LabUserName
          FOR XML PATH(''), 
          TYPE).value(N'./text()[1]', N'nvarchar(max)'), 1, 1, N'')
        FROM dbo.Labs AS l
    )
    UPDATE l
    SET LabUsers = x.x
    FROM dbo.Labs AS l
    INNER JOIN x ON l.LabID = x.LabID;
    

    As for the performance tests, I’d compare the above version with this variation:

    SELECT l.LabID, l.LabName, LabUsers = STUFF((SELECT N'•' + lu.LabUserName
      FROM dbo.LabUsers AS lu 
      WHERE lu.LabID = l.LabID
      ORDER BY lu.LabUserName
      FOR XML PATH('')), 1, 1, '')
    FROM dbo.Labs AS l;
    

    On my system I see the initial version at the top of this answer to be far more expensive. Also note that stuffing (no pun intended) these approaches into a user-defined function will bring it closer to the concatenation method @RThomas proposed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in a database. Table 1 includes an order number which
I have database in which data is imported from the other table. if data
I have a database which has a table with an XML column. The XML
What I would like to 'have' is someway to export my tables including the
I have a database which I regularly need to import large amounts of data
I have a database which contains picture data stored as a binary blob. The
I have a database which stores (among other things), the following pieces of information:
I have a database which needs to store year ranges (such as lifespan) which
I have a database which users should not be able to alter data in
I have a database which is synchronized against an external web source twice a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.