Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
PostgreSQL order by, numbers last

name2 could be string or number. I.e. 'one two' or 12

I would like to order names so that numbers to be last, i.e:

a a
b b
one two
1
22
select name1, name2 
from names 
order by name2

Q: how one would do it?

P.S. in oracle it is like this.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use REGEXP_REPLACE() function with '[^[:digit:]]*' posix, to determine whether the string has a digit or not in it, including an asterisk at the end to anchor a regular expression to the end of the source text :

SELECT name2
  FROM t
 ORDER BY REGEXP_REPLACE(name2,'[^[:digit:]]*',''), name2

Demo

over 4 years ago · Santiago Trujillo Report

0

You can use a boolean expression in order by, e.g.:

select name1, name2 
from names 
order by left(name2, 1) <= '9', name2

Db<>fiddle.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!