Erin Flynn
Erin Flynn

Reputation: 9

sort data in Ascending order POSTGRES SQL

I have data in the format (Before sorting)

"8 - 15days"    "0"
"0 - 24hrs" "0"
"above 30days"  "0"
"2 - 7days" "0"
"16 - 30days"   "0"

I need to sort the data in order like this(After sorting)

"0 - 24hrs" "0"
"2 - 7days" "0"
"8 - 15days"    "0"
"16 - 30days"   "0"
"above 30days"  "0"

Can anyone help me to sort the data. I tried order by but it is not sorting the data in the way I need.

Upvotes: 0

Views: 59

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 248215

If the column with the text in it is called x, you could use

ORDER BY x !~ '^[[:digit:]]',
         regexp_replace(x, '^([[:digit:]]*).*$', '0\1')::integer

Upvotes: 1

Related Questions