inquisitive_mind inquisitive_mind - 1 month ago 6
SQL Question

Filter records based on numeric value when column type is varchar

I have a column in a table with datatype varchar that stores version numbers.I need to filter and select only version numbers that are less than equal to 5

Input

5.0.0.330
Eclair
5.0.0
5.0.0.591
5.0.0.405
6.0.0.522
4.0.2
7.1.0.205
5.0.0.592
2.3.4-ez
4.2.2-2013-12-11-V1.0
4.6.0.304
nubernel-2.6.35_v0.0.1
2.1-update1
2.3


Output

5.0.0
4.0.2
2.3.4-ez
4.2.2-2013-12-11-V1.0
4.6.0.304
2.1-update1
2.3


I can get all the versions less than 5 by converting the first character of the varchar column.However I can't figure out how to include the 5.0.0 version in the result set.

select distinct os_ver,substring(os_ver,1,1)
from
dbo.mytable
where
os_ver like '[0-9]%' and cast (substring(os_ver,1,1) as int) < 5


This gives me all version less than 5 except the version 5.0.0

4.0.2
2.3.4-ez
4.2.2-2013-12-11-V1.0
4.6.0.304
2.1-update1
2.3

Answer
Select * 
 From  dbo.mytable 
 Where os_ver<='5.0.0'

Returns

os_ver
5.0.0
4.0.2
2.3.4-ez
4.2.2-2013-12-11-V1.0
4.6.0.304
2.1-update1
2.3