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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:12:07+00:00 2026-05-15T02:12:07+00:00

How can you sort a query using ORDER BY CASE WHEN REGEXP? or other

  • 0

How can you sort a query using ORDER BY CASE WHEN REGEXP? or other alternatives?
I don’t want to use UNION. Thank you

mysql> SELECT `floor_id`, `floor_number` FROM `floors`;
+----------+--------------+
| floor_id | floor_number |
+----------+--------------+
|        1 | 4            |
|        2 | 7            |
|        3 | G            |
|        4 | 19           |
|        5 | B            |
|        6 | 3            |
|        7 | A            |
+----------+--------------+


Expected result:
+----------+--------------+
| floor_id | floor_number |
+----------+--------------+
|        7 | A            |
|        5 | B            |
|        3 | G            |
|        6 | 3            |
|        1 | 4            |
|        2 | 7            |
|        4 | 19           |
+----------+--------------+
  • 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-15T02:12:08+00:00Added an answer on May 15, 2026 at 2:12 am

    Your use of REGEXP is pretty close. This should work:

    SELECT floor_id, floor_number FROM floors ORDER BY CASE
    WHEN floor_number REGEXP '[a-zA-Z]' THEN 0
    ELSE 0+floor_number END ASC, floor_number;
    

    The CASE statement ranks the lettered floors as 0 and uses 0+ to coerce the numeric floors into a number value that can be ordered. A second level sort by floor_number is then required so that the lettered floors order correctly as A,B,G. Without the 2nd level order the lettered floors would all be considered equivalent with a value of 0 and would not appear in a defined order.

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

Sidebar

Related Questions

I have a large amount of data to sort and query, and I can't
I have been trying to create a ListView which I can sort using drag
How do i implement a java comparator class which can sort the order of
I need to run a MySQL query where the order is determined by an
I can't get it to work. I'm using this query: my $user_questions = RoseDB::UserSecurityQuestion::Manager->get_user_security_questions(
I need to order a select query using a varchar column, using numerical and
I'm trying to sort a resultset using the SQL statement Order by using JPA,
I can sort by key or value, but I need it sorted by value,
The UNIX sort command can sort a very large file like this: sort large_file
Google Maps itself can sort of Highlight areas, when you are searching for example

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.