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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:50:22+00:00 2026-06-15T10:50:22+00:00

I have a large number of rows in an SQL Server 2008 db For

  • 0

I have a large number of rows in an SQL Server 2008 db

For each row I have 3 columns that I care about

A typical row looks like this:

AccountNumber    | basecode                      | subcode
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9689787209368901 | AQTXG AQTXG AQTXG AQTXG ACC5Z | ZQ596 ZQ596 ZQ596 ZQ655 ZC655

I need an SQL query that turns it into the following:

AccountNumber      |    basecode| subcode
+++++++++++++++++++++++++++++++++++++++++++
9689787209368901   |    AQTXG   | ZQ596
9689787209368901   |    AQTXG   | ZQ596
9689787209368901   |    AQTXG   | ZQ596
9689787209368901   |    AQTXG   | ZQ655
9689787209368901   |    ACC5Z   | ZC655
  • 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-15T10:50:22+00:00Added an answer on June 15, 2026 at 10:50 am

    Since you are using SQL Server, you can use a recursive query to split the data. IN this query, you can have an unknown number of items that are split with the space (' '), if your delimiter changes then this is easy to alter. Your query would be similar to this:

    ;with cte (AccountNumber, basecodeItem, basecode, subcodeItem, subcode) as
    (
      select AccountNumber,
        cast(left(basecode, charindex(' ',basecode+' ')-1) as varchar(50)) basecodeItem,
             stuff(basecode, 1, charindex(' ',basecode+' '), '') basecode,
        cast(left(subcode, charindex(' ',subcode+' ')-1) as varchar(50)) subcodeItem,
             stuff(subcode, 1, charindex(' ',subcode+' '), '') subcode
      from yourtable
      union all
      select AccountNumber,
        cast(left(basecode, charindex(' ',basecode+' ')-1) as varchar(50)) basecodeItem,
        stuff(basecode, 1, charindex(' ',basecode+' '), '') basecode,
        cast(left(subcode, charindex(' ',subcode+' ')-1) as varchar(50)) subcodeItem,
        stuff(subcode, 1, charindex(' ',subcode+' '), '') subcode
      from cte
      where basecode > '' or subcode > ''
    )
    select AccountNumber, basecodeItem, subcodeitem
    from cte;
    

    See SQL Fiddle with Demo

    The result is:

    |    ACCOUNTNUMBER | BASECODEITEM | SUBCODEITEM |
    -------------------------------------------------
    | 9689787209368900 |        AQTXG |       ZQ596 |
    | 9689787209368900 |        AQTXG |       ZQ596 |
    | 9689787209368900 |        AQTXG |       ZQ596 |
    | 9689787209368900 |        AQTXG |       ZQ655 |
    | 9689787209368900 |        ACC5Z |       ZC655 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large number of rows in an SQL Server 2008 db For
Edit: Im running SQL Server 2008 I have about 400,000 rows in my table.
i have a sql query that can bring back a large number of rows
I need to transfer a large number of rows from a SQL Server database
I have a large number of rows that I would like to copy, but
I have been running a sql query for large number of rows in a
I have a problem in SQL Server 2008 Reporting Services. The problem is that
Have some employee segmentation tasks that result in a large number of records (about
I'm using Postgres, and I have a large number of rows that need to
I have a very large number of rows (220,000) and 30 columns in an

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.