How to add values to an empty string, and to parse the string values using them for another task

minh tran 216 Reputation points
2021-12-15T14:47:38.523+00:00

Good morning,
I would like to seek your help and expertise regarding to how to add values from a table to an empty string , to parse the empty string looking for a particular string value and using each of them for another task such as inserting into another table.

I have a table such as Table Names contain columns last_name, and address.

I would like to add every address values in Table Names that the last names is "John" in to a declare target string of length 200.

At the end, my target string containing values; for example, ("1 Main String, GA 30033", "2 North Avenue, Hapeville 30324", "@4 North Druid Hill, Atlanta 30129").

I then plan to parse the values in my target string , and then add it each of the value
to a address tables .

I would like to seek your help if you can show me some sample code on how to achieve the above tasks.

Many Thanks for all of the help.
Du

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-12-15T15:15:22.633+00:00

    Consider one of approaches:

    declare @result varchar(200) = 
        (
            select a = trim(address)
            from Names
            where last_name = 'John'
            for xml path(''), root('d')
        )
    
    print @result
    

    It will build a string that contains all of addresses.

    Then you can split it into separate values (rows) using a query like this:

    declare @x as xml = @result
    
    select a.value('.', 'varchar(max)') as address from @x.nodes('/d/a') n(a)
    

  2. Isabellaz-1451 3,616 Reputation points
    2021-12-16T02:58:14.267+00:00

    Hi @minh tran

    Maybe you want to merge data from multiple column into one column ,right?
    Here is an example,hope it helps

    create table sourcetable  
    ( id integer identity(1,1),  
    last_name varchar(20),  
      address varchar(50))  
      
    insert into sourcetable(last_name,address)  
    select 'joe','Main String, GA 30033'  
    union all  
    select 'John','North Avenue, Hapeville 30324'  
    union all  
    select 'John','North Druid Hill, Atlanta 30129'  
      
    select * from sourcetable  
      
    select CONCAT('name:',last_name,'   ','address:',address) from sourcetable where last_name = 'John'  
    

    And if you want to merge all row values ,you can try this statement

    DECLARE @CHARSTRING VARCHAR(MAX)  
    SET @CHARSTRING =  ';WITH CTE AS(select CONTENT =  CONCAT(''name:'',last_name,''   '',''address:'',address) from sourcetable where last_name = ''John'')  
    (SELECT STRING_AGG(CONTENT,''||'') AS SSS FROM CTE)'  
    EXEC (@CHARSTRING)  
    

    Best Regards,
    Isabella


    If the answer is the right solution, please click "Accept Answer" and upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.