How to get MAX value of a version-number (varchar) column in T-SQL

Posted by Ogre Psalm33 on Stack Overflow See other posts from Stack Overflow or by Ogre Psalm33
Published on 2010-06-16T22:09:09Z Indexed on 2010/06/16 22:12 UTC
Read the original article Hit count: 514

Filed under:
|
|
|
|

I have a table defined like this:

Column:  Version     Message
Type:    varchar(20) varchar(100)
----------------------------------
Row 1:    2.2.6       Message 1
Row 2:    2.2.7       Message 2
Row 3:    2.2.12      Message 3
Row 4:    2.3.9       Message 4
Row 5:    2.3.15      Message 5

I want to write a T-Sql query that will get message for the MAX version number, where the "Version" column represents a software version number. I.e., 2.2.12 is greater than 2.2.7, and 2.3.15 is greater than 2.3.9, etc. Unfortunately, I can't think of an easy way to do that without using CHARINDEX or some complicated other split-like logic. Running this query:

SELECT MAX(Version) FROM my_table

will yield the erroneous result:

2.3.9

When it should really be 2.3.15. Any bright ideas that don't get too complex?

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about sorting