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

  • Home
  • SEARCH
  • 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 9182795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:38:47+00:00 2026-06-17T18:38:47+00:00

I have a column named MR which is a varchar. When I run a

  • 0

I have a column named MR which is a varchar. When I run a query with an ORDER BY it doesn’t seem to be ordered correctly.

select MR, LName, FName 
from users
order by MR

Results:

MR        | LNAME | FNAME
----------+-------+-------
1234-234  | HEN   | LO
2343MA2   | SY    | JACK
MR20001   | LINA  | MARY
MR200011  | TEST  | CASE
MR20002   | KO    | MIKE

Why does MR200011 show before MR20002?
Any Idea guys on how I can properly sort this? The format of MR is not fixed.

  • 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-17T18:38:48+00:00Added an answer on June 17, 2026 at 6:38 pm

    You are sorting by string, not by the value of the number. The character in position 7 is the difference that’s being compared:

    MR200011 
    MR20002 
          ^
    

    And because ‘2’ > ‘1’, this is the order you end up with. The 8th character is never compared, because the character-based sort order doesn’t depend on it.

    To ‘fix’ this issue, create a stored function which takes your varchar value, and returns a new ‘sort string’ which pads the numeric components to a fixed length.

    e.g.

    MR20002  -> MR0020002
    MR200011 -> MR0200011
    

    but more importantly, if you have two blocks of numbers, they don’t become corrupted:

    A1234-234  -> A000000001234-000000000234
    A1234-5123 -> A000000001234-000000005123
    

    The following function performs this transformation on sql-server – you’d have to adapt this function for mysql:

    create function dbo.get_numeric_sort_key(@value varchar(100)) 
        returns varchar(200)
    as
    begin
       declare @pad_characters varchar(12)
       declare @numeric_block varchar(12)
       declare @output varchar(200)
       set @pad_characters = '000000000000'
       set @output = ''
       set @numeric_block = ''
    
       declare @idx int
       declare @len int
       declare @char char(1)
       set @idx = 1
       set @len = len(@value)
       while @idx <= @len
       begin
         set @char = SUBSTRING(@value, @idx, 1)
         if @char in ('0','1','2','3','4','5','6','7','8','9') 
         begin
            set @numeric_block = @numeric_block + @char
         end
         else
         begin
            if (@numeric_block <> '')
            begin
              set @output = @output + right(@pad_characters + @numeric_block, 12)
              set @numeric_block = ''
            end
            set @output = @output + @char
         end
         set @idx = @idx + 1
       end
    
       if (@numeric_block <> '')
         set @output = @output + right(@pad_characters + @numeric_block, 12)
    
       return @output
    end
    

    Then change your order by clause to use the new function:

    select MR, LName, FName 
    from users 
    order by dbo.get_numeric_sort_key(MR)
    

    If you have a large amount of data, it would be worth adding a calculated field to the end of your table definition (populated by this function) so that you don’t have to do a scan every time you run this query.

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

Sidebar

Related Questions

I have a column named MR which is a varchar. When I run a
I have a table named b having a column j which is varchar(3) where
I have a tabel containing a column named parent which is able to store
I have a database table named 'student' in which there is one column named
I have an entity named 'account' which will have a username column and I
I have a column named thedate that is varchar. An example of a row
I have a table named KEYWORDS with a column named ENTRY of VARCHAR(10). Would
I have a column in my database named Created which is a datetime and
I have a table which has a VARCHAR(MAX) column, and I need to change
I have two or more tables which hold values under the same column name.

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.