1

I've tried the obvious select min(version) from t but this wouldn't work as the engine considers (and rightly so) that the string '1.12.0.0' is smaller than '1.2.0.0', which is obviously not the case when it comes to version numbers.

I know I can easily split the 4 numbers dynamically (using either computed columns or a view) and then I can just use select top 1 row from myview order by a asc, b asc, c asc, d asc

... but I was curious to know if there was an easier way to achieve this

Brann
  • 29,395
  • 31
  • 107
  • 159

1 Answers1

3

If your version numbers contain at most 4 numbers separated by a dot, you can abuse the parsename function:

select  top 1 *
from    Versions
order by
        cast(parsename(Version, 4) as int)
,       cast(parsename(Version, 3) as int)
,       cast(parsename(Version, 2) as int)
,       cast(parsename(Version, 1) as int)

Live example at SQL Fiddle.

Andomar
  • 216,619
  • 41
  • 352
  • 379