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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:40:24+00:00 2026-05-26T04:40:24+00:00

Here’s what I’m trying to do. I have these: Table1: Name | Surname |

  • 0

Here’s what I’m trying to do.

I have these:

Table1:

Name | Surname | Age | Location | ContactPeopleIds
John | Cobaing | 25  | Turkey   | 1234,1512,1661, 2366,
Jack | Maltean | 29  | Italy    | 6155,2333,1633,

Table2:

ID   | Name | LastName | Location
1234 | Meg  | Ryan     | US
1512 | Jesy | Jade     | US
1661 | John | Kradel   | US
2366 | Jack | Abdona   | Nigeria

TableIWant

Name | Surname | Age | Location | ContactPeopleNames
John | Cobaing | 25  | Turkey   | Meg Ryan, Jesy Jade, John Kradel, Jack Abdona

I have found a splitter function called fn_ParseText2Table(data, splitter) that creates a table from the data splitted with splitter char. (Reference here)

For example:

select *
from dbo.fn_ParseText2Table('1234,1512,1661,2366', ',')

function produces:

int_value | num_value | txt_value
null      | null      | 1234
null      | null      | 1512
null      | null      | 1661
null      | null      | 2366

But I couldn’t create a query using this.

I’m not sure to use t-sql or not.
I’ve tried to use common table expression but couldn’t manage that either.

If you can provide multiple solutions, it would be very kind to provide detail about their performance value differences.

  • 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-26T04:40:25+00:00Added an answer on May 26, 2026 at 4:40 am

    ok…

    When you suggested that you’d tried a CTE you where heading in the right direction.

    What you need to do however is chain 3 CTE’s together. Once you have the processing chain you then need to progressively pass things through it like a filter, first splitting the ID’s into a column of ints, then joining the ints on table2 to get the names, then recombining those names.

    As has been previously mentioned, whoever designed this designed it badly, but assuming your using MS-SQL server and T-SQL the following code will do what you need it to:

    DECLARE @tempString AS varchar(max)
    SET @tempString =''
    
    ;WITH firstCte AS
    (
        SELECT
            CAST('<M>' + REPLACE(contactpeopleids, ',','</M><M>') + '</M>' AS XML) AS Names
        FROM
            soTable1
    
        -- THIS WHERE CLAUSE MUST MATCH THE FINAL WHERE CLAUSE
        WHERE
            name = 'John'
            AND surname = 'Cobaing'
    )
    ,secondCte AS
    (
        SELECT
            Split.a.value('.','VARCHAR(100)') AS NameIds
    
        FROM
            firstCte
    
        CROSS APPLY Names.nodes('/M') Split(a)
    )
    ,thirdCte AS
    (
        SELECT
            t2.name + ' ' + t2.lastname AS theContactName
    
        FROM
            secondCte t1
    
        -- NOTE: IF THE IDS YOU EXTRACT FROM TABLE 1 DO NOT HAVE A MATCH IN TABLE 2 YOU WILL GET NO RESULT FOR THAT ID HERE!
        -- IF YOU WANT NULL RESULTS CHANGE THIS TO A 'LEFT JOIN'    
        INNER JOIN
            soTable2 t2 ON t1.NameIds = t2.id
    )
    SELECT
        @tempString = @tempString + ',' + theContactName
    
    FROM
        thirdCte
    
    ;
    -- The select substring is used to remove the leading ',' 
    SELECT 
        name,
        surname,
        age,
        location,
        SUBSTRING(@tempString,2,LEN(@tempString)) AS contactpeoplenames
    
    FROM
        soTable1
    
    WHERE
        name = 'John'
        AND surname = 'Cobaing'
    

    It’s probably not as elegant as it could be, and for ease of use you might want to wrap it up in a user defined function and pass the firstname and surname of the person to look up into it. If you do it that way, then you can use the function in a regular SQL select query to return rows direct out of table 1 into a view or another table.

    The fun part of it all is actually the way in which we trick SQL server into splitting the string. You’ll notice that we actually replace the ‘,’ with XML tags, then use the XML processing functions to make SQL server think that we are processing an XML string.

    SQL Server has had great routines for doing this kind of task sing the 2005 version, and allows for whole blocks of XML to be serialised and de-serialised to/from a varchar field directly in your db table, by making SQL server think it’s dealing with XML it does most of the hard work for us.

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

Sidebar

Related Questions

Here is what I am trying to achieve in PHP: I have this string:
Here is my problem...I have a page that loads a list of clients and
Here is an example: I have a file 1.js, which has some functions. I
Here is my situation: I am using telerik with winform. I have a dataset
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here's what I'm trying to accomplish with this program: a recursive method that checks
here is my configuration: http://domain.com (obviously fictitious name...) hosted on a server running Apache
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here is an example: I have the generic type called Account. I wish to

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.