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 958645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:54:00+00:00 2026-05-16T00:54:00+00:00

Hope someone can help – I am a novice SQL hacker (and very bad

  • 0

Hope someone can help – I am a novice SQL hacker (and very bad at it indeed!)

I have two tables on SQL Server 2005 TABLE 1 and TABLE2:

TABLE1

COL1         COL2
1            10
2            20
3            30
4            10
4            20
5            20
6            30
7            10
7            20

TABLE2

COL1         COL2
10            A
20            B
30            C

COL2 in TABLE2 is a character representation of the numerical data in COL2 TABLE1. I hope this is understandable?

I have worked out how to select COL1 and COL2 from TABLE1 and concatenate the results to show this:

COL1         COL2Concat
1            10
2            20
3            30
4            10, 20
5            20
6            30
7            10, 20, 30 

Using this:

SELECT  COL1,
        STUFF(( SELECT  ',' + CAST(a.COL2 AS VARCHAR(255)) AS [text()]
                FROM    TABLE1 a
                WHERE   a.COL1 = b.COL1
                ORDER BY a.COL2
              FOR
                XML PATH('')
              ), 1, 1, '') AS COL2Concat
FROM    TABLE1 b
GROUP BY COL1
ORDER BY COL1

But now I’d like to try and get the same result except use the data in COL2 TABLE2… i.e.:

COL1         COL2Concat
1            A
2            B
3            C
4            A, B
5            B
6            C
7            A, B, C 

Any ideas – I’m stuck to be honest as I have tried modifying the STUFF query, but it never seems to come out right…

  • 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-05-16T00:54:01+00:00Added an answer on May 16, 2026 at 12:54 am

    you could try…

    SELECT  COL1,
            STUFF(( SELECT  ',' + CAST((SELECT COL2
                                            FROM TABLE2
                                            WHERE TABLE2.COL1 = a.COL1) AS VARCHAR(255)) AS [text()]
                    FROM    TABLE1 a
                    WHERE   a.COL1 = b.COL1
                    ORDER BY a.COL2
                  FOR
                    XML PATH('')
                  ), 1, 1, '') AS COL2Concat
    FROM    TABLE1 b
    GROUP BY COL1
    ORDER BY COL1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
Hope someone can help me out with this one ! I have a sql
Hi hope someone can help, I have the following little problem: Ubuntu server 64bit
I have a really hard time understanding routes and I hope someone can help
Hope someone can help me with this.. I have a magento store up&running, via
I hope someone can help me out. I have a table that logs our
I hope someone can help me. My Problem: I have 2 sortable UL's. <ul
Hope someone can help, I'm not a programmer, but have been interested in exploring
Hope someone can help. I have a simple scenario where clicking checkboxes is driving
I hope someone can help... This issue has been discussed here and I have

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.