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

The Archive Base Latest Questions

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

I know that this question has been asked in various forms but my requirement

  • 0

I know that this question has been asked in various forms but my requirement happens to be a bit different.

Suppose I have a table that contains data as follows:

ID  NAME        VALUE

-----------------------------

1   ABC-2-2     X
2   PQRS-1-3    Y
3   ABC-3-2     Z
4   PQRS-1-4    A
5   PQRS-3-4    B
6   MNO-2-1     C
7   AAA-1       D
8   BBB-2       E
9   CCC-3       F

Now, the output that I’m expecting should look something like this:

ID  NAME        VALUE

-----------------------------
7   AAA-1       D
2   PQRS-1-3    Y
4   PQRS-1-4    A
8   BBB-2       E
6   MNO-2-1     C
1   ABC-2-2     X
9   CCC-3       F
3   ABC-3-2     Z
5   PQRS-3-4    B

Note that this is not a direct alpha-numeric sort. Instead, the value before the first “-” is ignored and the fields are sorted on what is after the first “-” in the name.

I’m not very familiar with PL/SQL and any kind of help on this would be appreciated.

Thanks.

PS: Note that this should work on both Oracle and MySQL.

  • 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-17T08:38:14+00:00Added an answer on June 17, 2026 at 8:38 am

    For your example this would suffice (Oracle syntax):

    ORDER BY SUBSTR(name,4)
    

    If the number of characters before the first hyphen can vary, you can do this (again Oracle syntax):

    ORDER BY SUBSTR(name,INSTR(name,'-')+1)
    

    However that won’t work if you have codes like:

    AAA-10-1
    AAA-8-1
    AAA-9-1
    

    and expect AAA-10-1 to appear after AAA-9-1. Then you will need to parse it further:

    ORDER BY LPAD(SUBSTR(name,INSTR(name,'-')+1, INSTR(name,'-',1,2)-INSTR(name,'-')-1),10,'0'),
             LPAD(SUBSTR(name,INSTR(name,'-',1,2)+1),10,'0')
    

    (NB I have used LPAD(x,10,’0′) to turn a value like ‘1’ into ‘0000000001’ and so on, rather than use TO_NUMBER since this could fail if there are any non-numerics in your data.)

    Example:

    with data as
    (
    select 'AAA-1' name from dual
    union all
    select 'PQR-1-4' name from dual
    union all
    select 'PQR-1-3' name from dual
    union all
    select 'AAA-10-10' name from dual
    union all
    select 'AAA-10-1' name from dual
    union all
    select 'AAA-9-10' name from dual
    union all
    select 'AAA-9-1' name from dual
    )
    select *
    from data
    ORDER BY LPAD(SUBSTR(name,INSTR(name,'-')+1, INSTR(name,'-',1,2)-INSTR(name,'-')-1),10,'0'),
             LPAD(SUBSTR(name,INSTR(name,'-',1,2)+1),10,'0');
    

    Output:

    NAME
    ---------
    PQR-1-3
    PQR-1-4
    AAA-9-1
    AAA-9-10
    AAA-10-1
    AAA-10-10
    AAA-1
    

    And if AAA-1 should come first:

    ORDER BY LPAD(SUBSTR(name,INSTR(name,'-')+1, INSTR(name||'-','-',1,2)-INSTR(name,'-')-1),10,'0'),
             LPAD(SUBSTR(name,INSTR(name||'-','-',1,2)+1),10,'0') nulls first
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this question has been asked many times ( but in different
I believe that this question has been asked in a few different forms, but
I know that this sort of question has been asked here before, but still
I know that this question has been asked over and over again, but nothing
I know that this question has been asked loads of times, but I have
I know that this question has already been asked, but I have strange problem
I know that this question has been asked many times before in different guises
This question has been asked several times in various forms but I haven't found
I know that this question has been asked multiple times already , but I
I know that this question has been asked too many times, but I think

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.