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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:30:22+00:00 2026-06-15T10:30: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

Certain basecodes need to be removed along with their corresponding sub codes. Both basecode and supcode are ordered lists delimited by spaces. I need a query that I input the base code and it deletes the basecode and subcode and leaves the rest of the the list intact.

Each row has the the same number of basecode and subcode items. The number of codes can vary from 0 to 45. The same basecode can appear multiple times. All fields are varchars.

So that the data ultimately (dynamically parses) out as so: (because there is a variable number of basecodes/subcode (pairs) per acct.

If I am trying to remove “ACC5Z” then I should have

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

If I am trying to remove “AQTXG” then I should have

AccountNumber    | basecode   | subcode
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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:30:23+00:00Added an answer on June 15, 2026 at 10:30 am

    Try this

    DECLARE @t TABLE (AccountNumber VARCHAR(100),Basecode VARCHAR(100),Subcode VARCHAR(100))
    INSERT INTO @t SELECT 9689787209368901,'AQTXG AQTXG AQTXG AQTXG ACC5Z','ZQ596 ZQ596 ZQ596 ZQ655 ZC655'
    
    DECLARE @BaseCodetoRemove VARCHAR(10) = 'AQTXG'
    
    ;WITH CTE AS(
    SELECT  
        AccountNumber
        ,LTRIM(RTRIM(SUBSTRING(Basecode, Number ,CHARINDEX(' ', Basecode + ' ', Number ) - Number))) AS Basecode
        , LTRIM(RTRIM(SUBSTRING(Subcode, Number ,CHARINDEX(' ', Subcode + ' ', Number ) - Number))) AS Subcode
    FROM @t 
    JOIN master..spt_values ON Number <= DATALENGTH(Basecode) + 1  AND type='P'
    AND SUBSTRING(' ' + Basecode, Number , 1) = ' ')
    ,CTE2 AS(
    SELECT * 
    FROM CTE 
    WHERE BaseCode <> @BaseCodetoRemove)
    
    SELECT AccountNumber
        ,STUFF((SELECT  ' ' + CAST(Basecode AS vARCHAR(MAX))
          FROM CTE2 t2 WHERE t1.AccountNumber = t2.AccountNumber
          FOR XML PATH('')),1,1,'')Basecode
    
        ,STUFF((SELECT ' ' + CAST(Subcode AS vARCHAR(MAX))
          FROM CTE2 t2 WHERE t1.AccountNumber = t2.AccountNumber
          FOR XML PATH('')),1,1,'')Subcode
    FROM CTE2  t1
    GROUP BY t1.AccountNumber
    
    • 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 an HTML table with a large number of rows, and I need

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.