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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:33:29+00:00 2026-06-07T12:33:29+00:00

In the database, I have various alpha-numeric strings in the following format: 10_asdaasda 100_inkskabsjd

  • 0

In the database, I have various alpha-numeric strings in the following format:

10_asdaasda
100_inkskabsjd
11_kancaascjas
45_aksndsialcn
22_dsdaskjca
100_skdnascbka

I want them to essentially be sorted by the number in front of the string and then the string name itself, but of course, characters are compared one by one and so the result of Order by name produces:

10_asdaasda
100_inkskabsjd
100_skdnascbka
11_kancaascjas
22_dsdaskjca
45_aksndsialcn

instead of the order I’d prefer:

10_asdaasda
11_kancaascjas
22_dsdaskjca
45_aksndsialcn
100_inkskabsjd
100_skdnascbka

Honestly, I would be fine if the strings were just sorted by the number in front. I’m not too familiar with PostgreSQL, so I wasn’t sure what the best way to do this would be. I’d appreciate any help!

  • 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-07T12:33:31+00:00Added an answer on June 7, 2026 at 12:33 pm

    The ideal way would be to normalize your design and split the two components of the column into two separate columns. One of type integer, one text.

    With the current table, you could:

    SELECT col
    FROM   tbl
    ORDER  BY (substring(col, '^[0-9]+'))::int  -- cast to integer
             , substring(col, '[^0-9_].*$');    -- works as text
    

    The same substring() expressions can be used to split the column.

    These regular expressions are somewhat fault tolerant:
    The first regex picks the longest numeric string from the left, NULL if no digits are found, so the cast to integer can’t go wrong.
    The second regex picks the rest of the string from the first character that is not a digit or ‘_’.

    If the underscore (_) is an unambiguous separator, split_part() is faster:

    SELECT col
    FROM   tbl
    ORDER  BY split_part(col, '_', 1)::int
            , split_part(col, '_', 2);
    

    db<>fiddle here

    See:

    • Split comma separated column data into additional columns
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scenario: I have various user's data stored in my database.
I have stored various records in the MySql database orkut. Now I want to
I have a database with 12,000 records with various attributes. One of them is
I have an ms-access database called db.mdb and it contains various table. I;m creating
I have a Yahoo! Group that has its own Database tables for various reasons.
Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships.
i have database with two values id and val so i want to ask
I have database application, I want to allow the user to restore the deleted
I have various database fields that are decrypted using the to_python method. The problem
I used mysqlhotcopy to dump a database full of tables. I know have various

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.