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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:55:27+00:00 2026-05-25T00:55:27+00:00

I have a table structure that contains a identifier column and a column that

  • 0

I have a table structure that contains a identifier column and a column that contains a deliminated string. What I would like to achieve is to insert the deliminated string into a new table as individual records for each of the values in the split deliminated string.

My table structure for the source table is as follows:

CREATE TABLE tablea(personID VARCHAR(8), delimStr VARCHAR(100))

Some sample data:

INSERT INTO tablea (personID, delimStr) VALUES ('A001','Monday, Tuesday')
INSERT INTO tablea (personID, delimStr) VALUES ('A002','Monday, Tuesday, Wednesday')
INSERT INTO tablea (personID, delimStr) VALUES ('A003','Monday')

My destination table is as follows:

CREATE TABLE tableb(personID VARCHAR(8), dayName VARCHAR(10))

I am attempting to create a Stored Procedure to undertake the insert, my SP so far looks like:

CREATE PROCEDURE getTKWorkingDays
  @pos integer = 1
  , @previous_pos integer = 0
  AS 
    BEGIN 
    DECLARE @value varchar(50)
            , @string varchar(100)
            , @ttk varchar(8)   
    WHILE @pos > 0 
      BEGIN 
        SELECT  @ttk = personID
                , @string = delimStr
                FROM    dbo.tablea
        SET @pos = CHARINDEX(',', @string, @previous_pos + 1) 
          IF @pos > 0 
            BEGIN       
              SET @value = SUBSTRING(@string, @previous_pos + 1, @pos - @previous_pos - 1)  
              INSERT  INTO dbo.tableb ( personID, dayName ) VALUES  ( @ttk, @value )        
              SET @previous_pos = @pos  
            END     
      END
      IF @previous_pos < LEN(@string) 
        BEGIN   
          SET @value = SUBSTRING(@string, @previous_pos + 1, LEN(@string))  
          INSERT  INTO dbo.tableb ( tkinit, dayName ) VALUES  ( @ttk, @value ) 
        END 
END

The data that was inserted (only 1 records out of the 170 or so in the original table which after spliting the deliminated string should result in about 600 or so records in the new table), was incorrect.

What I am expecting to see using the sample data above is:

personID    dayName
A001        Monday
A001        Tuesday
A002        Monday
A002        Tuesday
A002        Wednesday
A003        Monday

Is anyone able to point out any resources or identify where I am going wrong, and how to make this query work?

The Database is MS SQL Server 2000.

I thank you in advance for any assistance you are able to provide.

Matt

  • 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-25T00:55:28+00:00Added an answer on May 25, 2026 at 12:55 am

    Well your SELECT statement which gets the “next” person doesn’t have a WHERE clause, so I’m not sure how SQL Server will know to move to the next person. If this is a one-time task, why not use a cursor?

    CREATE TABLE #n(n INT PRIMARY KEY);
    
    INSERT #n(n) SELECT TOP 100 number     FROM [master].dbo.spt_values 
    WHERE number > 0    GROUP BY number    ORDER BY number;
    
    DECLARE 
        @PersonID VARCHAR(8),  @delimStr VARCHAR(100), 
        @str VARCHAR(100),     @c CHAR(1);
    
    DECLARE c CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY
        FOR SELECT PersonID, delimStr FROM dbo.tablea;
    
    OPEN c;
    
    FETCH NEXT FROM c INTO @PersonID, @delimStr;
    
    SET @c = ',';
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SELECT @delimStr = @c + @delimStr + @c;
    
        -- INSERT dbo.tableb(tkinit, [dayName])
        SELECT @PersonID, LTRIM(SUBSTRING(@delimStr, n+1, CHARINDEX(@c, @delimStr, n+1)-n-1))
            FROM #n AS n
            WHERE n.n <= LEN(@delimStr) - 1
            AND SUBSTRING(@delimStr, n.n, 1) = @c;
    
        FETCH NEXT FROM c INTO @PersonID, @delimStr;
    END
    
    CLOSE c;
    DEALLOCATE c;
    
    DROP TABLE #n;
    

    If you create a permanent numbers table (with more than 100 rows, obviously) you can use it for many purposes. You could create a split function that allows you to do the above without a cursor (well, without an explicit cursor). But this would probably work best later, when you finally get off of SQL Server 2000. Newer versions of SQL Server have much more flexible and extensible ways of performing splitting and joining.

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

Sidebar

Related Questions

I have a database structure that has a Person table which contains fields such
I have a table structure that looks like: <table> <tr id=row1> <td> <div>row 1
I currently have a table structure that looks something like this(some details omitted): ColumnName
How would one structure a table for an entity that can have a one
I have view that contains a table with the following structure: <table id=mappings> <thead>
I have a MySQL table that contains many rows. The table structure is as
I have a fairly simple dynamic html structure of a table that contains links
This is the MySQL table structure that I have: itemID (BIGINT) userID (BIGINT) wasAdded
I have a few classes, such as those that outline database table structure or
I have a table that has records with a structure similar to this.. ID

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.