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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:20:04+00:00 2026-06-12T03:20:04+00:00

I have some string values already populated in a nvarchar column. the format of

  • 0

I have some string values already populated in a nvarchar column. the format of the strings are like this:

For example: 16B, 23G, 128F, 128M etc…

I need to find out the maximum value from these, then generate the next one from code. The logic for picking up the maximum item is like the following:

  1. Pick up the string with the largest number.
  2. If multiple largest number, then pick up the string the largest alphabet among those.

For example, the largest string from the above series is 128M.

Now I need to generate the next sequence. the next string will have

  1. The same number as the largest one, but alphabet incremented by 1. I.E. 128N
  2. If the alphabet reaches to Z, then the number gets incremented by 1, and alphabet is A.
    for example, the next String of 128Z is 129A.

Can anyone let me know what kind of SQL can get me the desired string.

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

    Assuming:

    CREATE TABLE MyTable
        ([Value] varchar(4))
    ;
    
    INSERT INTO MyTable
        ([Value])
    VALUES
        ('16B'),
        ('23G'),
        ('128F'),
        ('128M')
    ;
    

    You can do:

    select top 1 
        case when SequenceChar = 'Z' then
            cast((SequenceNum + 1) as varchar) + 'A'
        else
            cast(SequenceNum as varchar) + char(ascii(SequenceChar) + 1)
        end as NextSequence
    from (
        select Value, 
            cast(substring(Value, 1, CharIndex - 1) as int) as SequenceNum, 
            substring(Value, CharIndex, len(Value)) as SequenceChar
        from (
            select Value, patindex('%[A-Z]%', Value) as CharIndex
            from MyTable
        ) a
    ) b
    order by SequenceNum desc, SequenceChar desc
    

    SQL Fiddle Example

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

Sidebar

Related Questions

I have some double values I want to convert to a string with this
I have some string like this: 04:51 05:09 05:29_ 05:49 06:09 06:29 06:49_ 07:09
I have string value and Type of some variable (For example int and 32
I have some String[] arrays, for example: ['a1', 'a2'] ['b1', 'b2', 'b3', 'b4'] ['c1']
I have some random string, let's say : s = This string has some
I have a ini file which contains some sample values like: [Section1] Value1 =
I have 2 arraylists contained objects like this: 1st arraylist contains string and double
I have a String with some value. I want to iterate over all classes
I have an issue while storing some special character values into db. For e.g.
HI, I have some string from XML file, and I I want to replace

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.