How to compare software versions using SQL Server?

后端 未结 16 2177
清酒与你
清酒与你 2020-12-03 07:30

When trying to compare software versions 5.12 to 5.8, version 5.12 is newer, however mathematically 5.12 is less than 5.8. How would I compare the two versions so that a new

16条回答
  •  攒了一身酷
    2020-12-03 08:10

    As suggested by AF you can compare the int part and then the decimal part .Apart from all the answers given there is one more way to do it using parsename .You could try something like this

     case when cast(@var as int)>cast(@var2 as int) then 'Y' 
     when cast(PARSENAME(@var,1) as int) > cast(PARSENAME(@var2,1) as int) THEN 'Y'
    
    
     Declare @var float
     Declare @var2 float
     set @var=5.14
     set @var2=5.8
     Select case when cast(@var as int)>cast(@var2 as int) then 'Y' 
     when cast(PARSENAME(@var,1) as int)> cast(PARSENAME(@var2,1) as int) THEN 'Y'
     else 'N' END
    

提交回复
热议问题