Try the following:
with a as ( select * from (values ('1.1.2'),('1.1.3'),('2.1.4 '), ('2.1.5'), ('2.2.1') ) as b(c) ) select c, PARSENAME(c,1),PARSENAME(c,2), PARSENAME(c,3) from a order by convert(int,PARSENAME(c,3)), convert(int,PARSENAME(c,2)), convert(int,PARSENAME(c,1))
Inspired by: http://www.sql-server-helper.com/tips/sort-ip-address.aspx
with a as ( select * from (values ('1.1.2'),('1.1.3'),('2.1.4 '), ('2.1.5'), ('2.2.1') ) as b(c) ), x as ( select c, convert(int,PARSENAME(c,3)) * 100 + convert(int,PARSENAME(c,2)) * 10 + convert(int,PARSENAME(c,1)) * 1 as the_value from a ) select c from x where the_value = (select MAX(the_value) from x)
In software development, it is typical to find a minor version number that has two digits in it, the version number does not have any bearing with a number, so version 1.12 is greater than 1.5; to compensate for this, you must substitute the numbers accordingly:
-- Use this, the query above is not future-proof :-) with a as ( select * from (values ('2.1.4 '), ('2.1.12'), ('2.1.5'), ('2.2.1') ) as b(c) ), x as ( select c, convert(int,PARSENAME(c,3)) * 100*100*100 + convert(int,PARSENAME(c,2)) * 100*100 + convert(int,PARSENAME(c,1)) * 100 as the_value from a ) select c, the_value from x order by the_value
Output:
2.1.4 2010400 2.1.5 2010500 2.1.12 2011200 2.2.1 2020100
If you do not take this into account (as with the following request):
with a as ( select * from (values ('2.1.4 '), ('2.1.12'), ('2.1.5'), ('2.2.1') ) as b(c) ), x as ( select c, convert(int,PARSENAME(c,3)) * 100 + convert(int,PARSENAME(c,2)) * 10 + convert(int,PARSENAME(c,1)) * 1 as the_value from a ) select c, the_value from x order by the_value; -- KorsG answer has a bug too with a as ( select * from (values ('2.1.4 '), ('2.1.12'), ('2.1.5'), ('2.2.1') ) as b(c) ), x as ( select c, CAST(REPLACE(c, '.', '') AS int) as the_value from a ) select c, the_value from x order by the_value
These two queries will lead to the same (wrong) conclusion:
c the_value 2.1.4 214 2.1.5 215 2.2.1 221 2.1.12 222
The values 2.2.1 and 2.1.12 overlap. This also happens when you simply delete points and directly convert the resulting string to int. 2.1.12 become two thousand one hundred twelve, 2.2.1 become two hundred twenty one. 2.2.1 more than 2.1.12, not less