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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:02:37+00:00 2026-05-16T07:02:37+00:00

I have two tables which both contain ‘pipe separated values’ e.g: Table 1: DataField_A

  • 0

I have two tables which both contain ‘pipe separated values’

e.g:

Table 1:

 DataField_A

 item 1|item 2|item 3|etc.....

Table 2:

 DataField_A

 item 7|item 5|item 3|etc.....

I need to merge Table 2 into table 1 such that Table 2 contains all items across both tables.

Doing this pro-grammatically would be a simple matter of looping through each item in table 2 and adding to table 1 if it does not exist in table 1.

How can this be done in SQL as a stored procedure?

  • 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-16T07:02:37+00:00Added an answer on May 16, 2026 at 7:02 am

    I’ve used a parsing function (the example I’m using comes from here) to parse the string in Table1. Then I use that function in a CTE to find missing elements in Table2 and merge the data.

    /* Helper function to parse delimited string */
    CREATE FUNCTION [dbo].[fnParseStringTSQL] (@string NVARCHAR(MAX),@separator NCHAR(1))
    RETURNS @parsedString TABLE (string NVARCHAR(MAX))
    AS 
    BEGIN
       DECLARE @position int
       SET @position = 1
       SET @string = @string + @separator
       WHILE charindex(@separator,@string,@position) <> 0
          BEGIN
             INSERT into @parsedString
             SELECT substring(@string, @position, charindex(@separator,@string,@position) - @position)
             SET @position = charindex(@separator,@string,@position) + 1
          END
         RETURN
    END
    go
    
    /* Set up some sample data */
    declare @Table1 table (
        id int,
        DataField_1A varchar(500)
    )
    
    declare @Table2 table (
        id int,
        DataField_2A varchar(500)
    )
    
    insert into @Table1
        (id, DataField_1A)
        select 1, 'item 1|item 2|item 3'
        union
        select 2, 'item A|item B|item C|item D|item Z'
    
    insert into @Table2
        (id, DataField_2A)
        select 1, 'item 7|item 5|item 3'
        union
        select 2, 'item A|item Y|item Z'
    
    /* data before the update */
    select * from @Table2
    
    /* boolean to ensure loop executes at least once */
    declare @FirstLoop bit
    set @FirstLoop = 1
    
    /* Do the updates */
    while (@FirstLoop = 1 or @@ROWCOUNT <> 0) begin 
        set @FirstLoop = 0
    
        ;with cteMissingItems as (
        select t2.id, p.string
            from @Table2 t2
                inner join @Table1 t1
                    on t2.id = t1.id
                cross apply dbo.fnParseStringTSQL(t1.DataField_1A,'|') p
            where charindex(p.string, t2.DataField_2A) = 0
        )
        update t2
            set t2.DataField_2A = t2.DataField_2A + '|' + mi.string
            from @Table2 t2
                inner join cteMissingItems mi
                    on t2.id = mi.id
    end /* while */
    
    /* Prove the update worked */
    select * from @Table2
    
    /* Clean up */
    drop function dbo.fnParseStringTSQL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 485k
  • Answers 485k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use grep and sed to do this: pax>… May 16, 2026 at 7:42 am
  • Editorial Team
    Editorial Team added an answer here is my resources.groovy import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH // Place… May 16, 2026 at 7:42 am
  • Editorial Team
    Editorial Team added an answer puts "Hello World" test = Thread.new do while true puts… May 16, 2026 at 7:42 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.