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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:53:10+00:00 2026-05-15T09:53:10+00:00

The query… select distinct name from myTable returns a bunch of values that start

  • 0

The query…

select distinct name from myTable 

returns a bunch of values that start with the following character sequences…

ADL*
FG*
FH*
LAS*
TWUP*

Where ‘*’ is the remainder of the string.

I want to do an order by that sorts in the following manner…

ADL*
LAS*
TWUP*
FG*
FH*

But then I also want to sort within each name in the standard order by fashion. So, an example, if I have the following values

LAS-21A
TWUP-1
FG999
FH3
ADL99999
ADL88888
ADL77777
LAS2

I want it to be sorted like this…

ADL77777
ADL88888
ADL99999
LAS2
TWUP-1
FG999
FH3

I initially thought I could accomplish this vias doing an order by decode(blah) with some like trickery inside of the decode but I’ve been unable to accomplish it. Any insights?

  • 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-05-15T09:53:11+00:00Added an answer on May 15, 2026 at 9:53 am

    Goofy and verbose, but should work:

    select name, case when substr (name, 1, 3) = 'ADL'  then 1  
                      when substr (name, 1, 3) = 'LAS'  then 2
                      when substr (name, 1, 4) = 'TWUP' then 3
                      when substr (name, 1, 2) = 'FG'   then 4
                      when substr (name, 1, 2) = 'FH'   then 5
                      else 6
                 end SortOrder
    from myTable
    order by 2, 1;
    

    Not sure if 6 is the correct place to sort the other items, but it is obvious how to fix that. At least it is clear what is going on, even if I have no idea why you are doing it this way.

    EDIT: If these are the only values, you could change lines 4 and 5:

    select name, case when substr (name, 1, 3) = 'ADL'  then 1  
                      when substr (name, 1, 3) = 'LAS'  then 2
                      when substr (name, 1, 4) = 'TWUP' then 3
                      when substr (name, 1, 1) = 'F'    then 4
                      else 6
                 end SortOrder
    from myTable
    order by 2, 1;
    

    ANOTHER EDIT: And again, if these are the only values, you can simplify even more. Since the only one out of order is the F* series, you can force them to the end, and use the actual first letter for all the others. This is simpler, but relies too much on the exact values for my preference. On the other hand, it does remove many of the seemingly unnecessary calls to substr :

    select name, case when substr (name, 1, 1) = 'F'  then 'Z'
                      else name
                 end SortOrder
    from myTable
    order by 2, 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Query: SELECT DISTINCT ([Equipment List].ID) AS Expr1, [Job Assignments].Job FROM [Equipment List] LEFT JOIN
Query : select i.Name,ri.Country,ri.State,ri.City from Information as i join ResidenceInformation as ri order by
Query: SELECT product_id, name FROM product WHERE barcode in (681027, 8901030349379, 679046, 679047, 679082,
Query 1: select distinct email from mybigtable where account_id=345 takes 0.1s Query 2: Select
Query select min Price, min year from tblProduct returns empty () results. I only
Query: SELECT id, name, FROM users u WHERE **id <> 0** LIMIT 50 OFFSET
(dynamic query...) select * from myTable where ( <--- @param1 IS NULL OR (
Query 1: SELECT * FROM user_d1 WHERE EXISTS (SELECT 1 FROM `user_d1` WHERE birthdate
Query: SELECT u.user_id,r.totalVotes votes,r.totalPoints rating,@row:=@row+1 rank FROM mismatch_user u LEFT JOIN ratingItems r ON
The query is: SELECT * FROM `news` ORDER BY `id` LIMIT ($curr_page * 5),

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.