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

  • SEARCH
  • Home
  • 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 290695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:01:17+00:00 2026-05-12T06:01:17+00:00

I have to deal with a mssql database and the information given in a

  • 0

I have to deal with a mssql database and the information given in a table like this:

Users:

ID Name    Countries
--------------------
1  User1   1,2,3
2  User2   2,5

Countries:
ID Country
----------
1  Australia
2  Germany
3  USA
4  Norway
5  Canada

Now, what i am looking for is a select statement that will give me a result like this:

Result:
ID User   CountriesByName
-----------------------------
1  User1  Australia, Germany, USA
2  User2  Germany, Canada

I’d prefer a solution that won’t depend on special MSSQL syntax over something special, but there is no way for me to use some LINQ-magic 🙁

  • 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-12T06:01:17+00:00Added an answer on May 12, 2026 at 6:01 am

    First, you’ll need to split that string up. Here’s a workable split function:

    Create Function [dbo].[split]
        (@input   varChar(8000)        -- List of delimited items
        ,@delimit varChar(8000) = ',') -- delimiter that separates items
    Returns @List Table ([item] varChar(8000)) As
    Begin
    
    Declare @item VarChar(8000);
    
    while charIndex(@delimit, @input, 0) <> 0 Begin
    
        Select
            @item  = rTrim(lTrim(subString(@input, 1, charIndex(@delimit, @input, 0) - 1))),
            @input = rTrim(lTrim(subString(@input, charIndex(@delimit, @input, 0) + Len(@delimit), Len(@input))));
    
        If Len(@item) > 0 Insert Into @List Select @item
    
    End
    
    If Len(@input) > 0 Insert Into @List Select @input
    
    Return;
    
    End
    

    Then you’ll need to join the values back to your countries table, and re-join them. This will get you most of the way:

    Select ID
          ,[Name] as [User]
          ,(
            Select [country] + ', '
            From [Countries]
            Where [ID] In (
                Select Cast([item] As Integer)
                From dbo.split(U.Countries, ',')
                Where IsNumeric(item) = 1)
            Order By [country]
            For XML Path('')) As [CountriesByName]
    From [Users] As U
    

    However, that leaves a trailing comma. You may want to remove that on some other layer, but just in case you MUST do it in SQL, this should work:

    Select ID
          ,[User]
          ,Left([CountriesByName], Len([CountriesByName]) - 1) As [CountriesByName]
    From (
        Select ID
              ,[Name] as [User]
              ,(
                Select [country] + ', '
                From [Countries]
                Where [ID] In (
                    Select Cast([item] As Integer)
                    From dbo.split(U.Countries, ',')
                    Where IsNumeric(item) = 1)
                Order By [country]
                For XML Path('')) As [CountriesByName]
        From [Users] As U) As [Results]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

the deal is this: I have a MySQL database that is built in this
I have developed a grails application that stores a great deal of information. Currently,
I have a MySQL database with a date_of_birth field. The data stored in this
I have a database model diagram created in MS Visio which I would like
I need help to model a table structure for my database. I have two
I am working on application which can deal with multiple database servers like "MySQL"
So, here's the deal. I have an html table that I want to populate.
I have to deal with an API which need to be provided a DataSource
I have a deal with a 3rd party web page. And If I click
I do have to deal with very large plain text files (over 10 gigabytes,

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.