By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

how to sort the version number in postgres

P: 1
i am fairly new in postgresql, so if anyone can help me would be great

if i simply do:

select ver_no
from version
order by ver_no

the result will be something like this:


so as you can see first 3 lines are ok, but how to make come before 1.4.11 because the third level "3" is smaller than "11". i understand they are stored as char so i used split_part function to separate each numbers between "." in a separate column. but when i try to convert those column into integer, i am getting an error msg saying: ERROR: invalid input syntax for type numeric: " "

here is my code:

select ver_no, duedate, status,
to_number(split_part(ver_no, '.', 2), '9999') a,
to_number(split_part(ver_no, '.', 3), '9999') b,
to_number(split_part(ver_no, '.', 4), '9999') c,
to_number(split_part(ver_no, '.', 5), '9999') d

from version

order by a,b,c,d

I am not sure if i am heading towards wrong direction but can someone please suggest or give me some other ideas to sort this.

PS: i found some good solution in SQL Server but those commands are not used in postgreSQL.
Sep 30 '11 #1
Share this Question
Share on Google+
1 Reply

P: 8
SELECT ver_no FROM version ORDER BY string_to_array(ver_no, '.', '')::int[]

should work.
Nov 10 '11 #2

This discussion thread is closed

Replies have been disabled for this discussion.