0

This is a follow up question to an earlier post. I have a column with stings & numbers that I would like to order in sequential order.

Say the customer_id is something like: cust-1-2, cust-10-1, cust-2-1, cust-1-1, cust-3-1

I want to order it by the first number in the string, and then by the second. How can I do this?

I want my result to be ordered like: cust-1-1, cust-1-2, cust-2-1, cust-3-1, cust-10-1

GMB
  • 216,147
  • 25
  • 84
  • 135
nz426
  • 91
  • 1
  • 10

1 Answers1

4

If the format of the string is consistent, you can use split_part():

order by 
    (split_part(customer_id, '-', 2))::int,
    (split_part(customer_id, '-', 3))::int

Demo on DB Fiddle

GMB
  • 216,147
  • 25
  • 84
  • 135