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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:19:20+00:00 2026-06-13T00:19:20+00:00

Using VB.Net and SQL Server Table1 Id Value ….. 1001P0010001 100 1001P0010002 200 1001P0010003

  • 0

Using VB.Net and SQL Server

Table1

Id             Value .....

1001P0010001   100
1001P0010002   200
1001P0010003   300
1001P0010004   400
...

I have n columns and rows in table1, from the table1, I want to copy all the column details with new id no…

id no is like this 1001P0020001, 1001P0020002, .......

P002 is next id, P003 is the next Id…..

The first 4 digits and last 4 digits will remain as read from table1, middle 4 digits should change to next series

Expected output

Id             Value .....

1001P0010001   100
1001P0010002   200
1001P0010003   300
1001P0010004   400
1001P0020001   100
1001P0020002   200
1001P0020003   300
1001P0020004   400
...

Which the best way to do this?

I can do it in VB.Net or a SQL query…? Please suggest ways of doing this.

  • 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-13T00:19:21+00:00Added an answer on June 13, 2026 at 12:19 am
    CREATE TABLE CocoJambo (
        Id  CHAR(12) NOT NULL,
        Value INT NULL,
        CHECK( Id LIKE '[0-9][0-9][0-9][0-9][A-Z][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' )
    );
    GO
    
    CREATE UNIQUE INDEX IUN_CocoJambo_Id
    ON CocoJambo (Id);
    GO
    
    INSERT  CocoJambo (Id, Value)
              SELECT '1001P0010001', 100
    UNION ALL SELECT '1001P0010002', 200
    UNION ALL SELECT '1001P0010003', 300
    UNION ALL SELECT '1001P0010004', 400
    UNION ALL SELECT '1001P0020001', 100
    UNION ALL SELECT '1001P0020002', 200
    UNION ALL SELECT '1001P0020003', 300
    UNION ALL SELECT '1001P0020004', 400;
    GO
    
    -- Test 1: generating a single Id
    DECLARE @Prefix CHAR(5),
            @Sufix CHAR(4);
    SELECT  @Prefix = '1001P',
            @Sufix = '0001';
    
    BEGIN TRAN
    
    DECLARE @LastGeneratedMiddleValue  INT,
            @LastValue INT;
    
    SELECT  @LastGeneratedMiddleValue = y.MiddleValue,
            @LastValue = y.Value
    FROM 
        (
        SELECT  x.MiddleValue, x.Value,
                ROW_NUMBER() OVER(ORDER BY x.MiddleValue DESC) AS RowNum
        FROM 
            (
            SELECT  CONVERT(INT,SUBSTRING(a.Id,6,3)) AS MiddleValue, a.Value
            FROM    CocoJambo a WITH(UPDLOCK) -- It will lock the rows (U lock) during transaction
            WHERE   a.Id LIKE @Prefix+'%'+@Sufix
            ) x
        ) y
    WHERE   y.RowNum=1;
    
    SELECT  @LastGeneratedMiddleValue  = ISNULL(@LastGeneratedMiddleValue ,0)
    SELECT  @Prefix
            +RIGHT('00'+CONVERT(VARCHAR(3),@LastGeneratedMiddleValue +1),3)
            +@Sufix AS MyNewId,
            @LastValue AS Value
    
    COMMIT TRAN;
    GO
    
    -- Test 2: generating many Id's
    BEGIN TRAN
    
    DECLARE @Results TABLE (
        Prefix CHAR(5) NOT NULL,
        Sufix CHAR(4) NOT NULL,
        LastGeneratedMiddleValue  INT NOT NULL,
        LastValue INT NULL
    );
    INSERT  @Results (Prefix, Sufix, LastGeneratedMiddleValue, LastValue)
    SELECT  y.Prefix, y.Sufix, y.MiddleValue, y.Value
    FROM 
        (
        SELECT  x.Prefix, x.MiddleValue, x.Sufix, x.Value,
                ROW_NUMBER() OVER(PARTITION BY x.Prefix, x.Sufix ORDER BY x.MiddleValue DESC) AS RowNum
        FROM 
            (
            SELECT  SUBSTRING(a.Id,1,5) AS Prefix,
                    CONVERT(INT,SUBSTRING(a.Id,6,3)) AS MiddleValue,
                    SUBSTRING(a.Id,9,4) AS Sufix,
                    a.Value
            FROM    CocoJambo a WITH(UPDLOCK) -- It will lock the rows (U lock) during transaction
            ) x
        ) y
    WHERE   y.RowNum=1;
    
    SELECT  r.*, 
            r.Prefix
            +RIGHT('00'+CONVERT(VARCHAR(3),r.LastGeneratedMiddleValue +1),3)
            +r.Sufix AS MyNewId,
            r.LastValue AS Value
    FROM    @Results r;
    
    COMMIT TRAN;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2005 and VB.Net Table1 ID Name Dept 001 Raja IT 002
Using VB.Net and SQL Server I want to compare the textbox value with table
Using SQL Server 2005, vb.net Table1 Name FromDate ToDate Raja 12/02/2010 14/02/2010 Ravi 14/02/2010
Using VB.Net and Sql Server I want to check the user Entry Value. The
If I am using .Net and SQL Server 2008, what is the best way
Using VB.NET and SQL Server 2005 In my software I am using login page
I am using .Net 2.0 + SQL Server 2005 Enterprise + VSTS 2008 +
I am using .Net 2.0 + SQL Server 2005 Enterprise + VSTS 2008 +
I'm using VB.NET and Microsoft SQL Server 2005 to create an application. I was
I maintain a legacy ASP.Net Web application (using .Net 2.0 + SQL Server 2005

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.