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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:26:33+00:00 2026-06-10T03:26:33+00:00

Trying to migrate from an old database to a new one in which the

  • 0

Trying to migrate from an old database to a new one in which the way data is stored is a bit different.

In one specific case I have a column with semicolon separated values that I would like to separate into multiple rows.

Here is an example:

SELECT 
    p.idperson, 
    p.roleperson
FROM person p

The TSQL above generates the following output

idperson roleperson
1001    ;214401;
1002    ;214201;214401;
1003    ;212101;

I would like to convert this to:

idperson roleperson
1001    214401
1002    214401
1002    214201
1003    212101

That is, I want to split the row with multiple values into two rows. Is this possible without creating cursors or loops?

  • 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-06-10T03:26:35+00:00Added an answer on June 10, 2026 at 3:26 am

    While writing this post I figured out an easy way to do this using cross apply and a custom split function. So instead of just asking the question I’ll post how I solved it using this self contained SQL too 😉

    -- set up a split function
    GO
    CREATE FUNCTION [dbo].[TempSplit] (@sep char(1), @s varchar(512))
    RETURNS table
    AS
    RETURN (
        WITH Pieces(pn, start, stop) AS (
          SELECT 1, 1, CHARINDEX(@sep, @s)
          UNION ALL
          SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
          FROM Pieces
          WHERE stop > 0
        )
        SELECT pn,
          SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
        FROM Pieces
      )
    GO
    
    -- set up some test data
    DECLARE @personroles TABLE 
        (
            idperson INT,
            rawroleoptions NVARCHAR(MAX)
        )
    
    INSERT INTO @personroles VALUES (1, ';1;2;3;')
    INSERT INTO @personroles VALUES (2, ';4;5;6;')
    INSERT INTO @personroles VALUES (3, ';7;')
    
    -- the actual work --
    ;WITH data AS
    (
        SELECT 
            p.idperson,
            p.rawroleoptions
    
        FROM @personroles p
    )
    SELECT * FROM data r
    CROSS APPLY  
        (SELECT s AS [ExtractedValue] FROM dbo.TempSplit(';',r.rawroleoptions) WHERE LEN(s)>0) d
    
    -- clean up --
    GO
    DROP FUNCTION dbo.TempSplit
    GO
    

    After running the above, this is what SQL outputs.

    Values split into new rows

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

Sidebar

Related Questions

i'm trying to migrate some data from two tables in an OLD database, to
I am trying to migrate data from an Informix database to SQL Server 2008.
I'm trying to write a SQL script to migrate data from some old table
I'm trying to migrate my application from the old App Engine db to ndb.
I am trying to migrate a database from SQL Server 2008 to SQL Azure.
I am trying to migrate some code from an old naming scheme to the
I'm trying to write Linq-To-SQL queries in LinqPad to help migrate users from old
I am trying to migrate a dataset from an old spreadsheet based system to
Im trying to migrate an old project from Delphi 5 to Delphi XE. When
Trying to migrate from MSSQL to MySQL. This Stored Proc is creating a temp

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.