In Oracle, just using the ORDER BY
does not sort version numbers.
My Version_Number
field is declared as a VARCHAR
and I cannot change it.
For Example: The following versions :
1.20
1.9
1.18
1.13
1.5
1.11
2.0
1.8
1.3
1.2
1.1
1.0
1.10
should be sorted as
2.0
1.20
1.18
1.13
1.11
1.10
1.9
1.8
1.5
1.3
1.2
1.1
1.0
I have researched several posts but none of them seem to really serve my purpose or the answers were intended for SQL Server, etc and not Oracle. I came across this particular sql which seemed to look like it worked.
select version_number from mytable
order by lpad(version_number, 4) desc;
which sorted the versions in this fashion:
1.20
1.18
1.13
1.11
1.10
2.0
1.9
1.8
1.5
1.3
1.2
1.1
1.0
I believe this sql statement works for SQL Server
:
select version_number from mytable
order by cast ('/' + replace(version_number , '.', '/') + '/' as hierarchyid) desc;
However, this does not work with Oracle. Is there an alternative to hierarchyid in Oracle?
Is there anyone who can come up with a SQL to sort these versions accurately?
I have already seen the posts mentioned below (links attached). So kindly do not tell me that this post is a duplicate.
SQL sort by version "number", a string of varying length
How Can I Sort A 'Version Number' Column Generically Using a SQL Server Query
Version number sorting in Sql Server
mysql sorting of version numbers
and many more.