So far, I’ve come up with
ORDER BY SUBSTRING(COLUMN FROM '[A-Z]+'), COALESCE(SUBSTRING(COLUMN FROM '(?<=-)[0-9]+')::INTEGER, 0), SUBSTRING(COLUMN FROM '(?<=()[0-9]+')::BIGINT
Outcome | Desired Outcome |
---|---|
AC-1 | AC-1 |
AC-2 (1) | AC-2 |
AC-2 (2) | AC-2 (1) |
AC-2 | AC-2 (2) |
AC-3 (1) | AC-3 (1) |
AC-3 (2) | AC-3 (2) |
AC-3 (3) | AC-3 (3) |
3
Answers
That is simple: just define the appropriate collation.
With old versions of the ICU library, you might have to define the
LOCALE
as@colNumeric=true
.Using your current data; I created fiddle.
The following query shall work for you :
It gives me desired output as :
Please replace
my_column
with your actualcolumn_name
andmytable
with actualtable_name
As with most SQL engines, the default behavior of postgresql when comparing NULLs to other values is to put NULLs last, so when the optional number preceded by a left parenthesis fails to match and becomes NULL, it gets placed last.
You can change the behavior to put NULLs first with the
NULLS FIRST
option: