2017-09-03 102 views
2

我有大约2000多个站点维护运行Oracle11g的内部应用程序的不同版本。版本比较

表中有一个参数来维护版本。 COl_1与值如5.2.4 or 6.86 or 7.2.0.1等。我需要比较两个网站版本,如(6.2.3site1大于site2这可能是6.8)。数据类型是Varchar2。

最简单的方法是什么?有没有内置的东西?基于结果我必须执行几个插入&合并。

那么,如何在oracle sql查询中比较5.2.4 > 6.2.1.4

在此先感谢。

+0

你知道最多版本的零件数量吗?那么,我问点数? –

+0

并根据你的例子它应该是6.2.1.4> 5.2.4? –

+0

最多3个点。但有些会有2位数字(6.3.95)。是的,5.2.4 <6.2.1.4我的意思是site1.col1> site2.col1?谢谢 – MKS

回答

2

您可以使用以下查询将包含版本的字符串拆分为个别部分,然后您可以对记录进行排序和比较。

SELECT x, 
     to_number(regexp_substr(x, '\d+', 1, 1)) As x_1, 
     to_number(regexp_substr(x, '\d+', 1, 2)) As x_2, 
     to_number(regexp_substr(x, '\d+', 1, 3)) As x_3, 
     to_number(regexp_substr(x, '\d+', 1, 4)) As x_4, 
     to_number(regexp_substr(x, '\d+', 1, 5)) As x_5, 
     to_number(regexp_substr(x, '\d+', 1, 6)) As x_6 
FROM table123 
order by 
    2 nulls first, 
    3 nulls first, 
    4 nulls first, 
    5 nulls first, 
    6 nulls first, 
    7 nulls first 

演示:http://sqlfiddle.com/#!4/60df0/4

|   X | X_1 | X_2 | X_3 | X_4 | X_5 | X_6 | 
|------------|-----|-----|--------|--------|--------|--------| 
|  1.1.1 | 1 | 1 |  1 | (null) | (null) | (null) | 
|  1.1.15 | 1 | 1 |  15 | (null) | (null) | (null) | 
|  2.7.1 | 2 | 7 |  1 | (null) | (null) | (null) | 
|  2.7.10 | 2 | 7 |  10 | (null) | (null) | (null) | 
|  3.1..1 | 3 | 1 |  1 | (null) | (null) | (null) | 
|  4.1.1 | 4 | 1 |  1 | (null) | (null) | (null) | 
|  6.4.2 | 6 | 4 |  2 | (null) | (null) | (null) | 
|  9.1 | 9 | 1 | (null) | (null) | (null) | (null) | 
|  9.1.2 | 9 | 1 |  2 | (null) | (null) | (null) | 
|  9.1.10 | 9 | 1 |  10 | (null) | (null) | (null) | 
| 10.1.1.2.4 | 10 | 1 |  1 |  2 |  4 | (null) | 
|  15.1.3 | 15 | 1 |  3 | (null) | (null) | (null) | 
|  21.1.1 | 21 | 1 |  1 | (null) | (null) | (null) | 
|  23.1.2 | 23 | 1 |  2 | (null) | (null) | (null) | 
| 23.1.10 | 23 | 1 |  10 | (null) | (null) | (null) | 
|  30.1.1 | 30 | 1 |  1 | (null) | (null) | (null) | 
|  31.1.1 | 31 | 1 |  1 | (null) | (null) | (null) | 
|  41.1 | 41 | 1 | (null) | (null) | (null) | (null) | 
0

你可以离开垫的主要,次要和补丁版本和将它们连接起来,总字符串比较:

SELECT 
    CASE 
    WHEN lpad(regexp_substr('6.2.1.4', '\d+', 1, 1),10,'0') 
     ||lpad(regexp_substr('6.2.1.4', '\d+', 1, 2),10,'0') 
     ||lpad(regexp_substr('6.2.1.4', '\d+', 1, 3),10,'0') 
     ||lpad(regexp_substr('6.2.1.4', '\d+', 1, 4),10,'0') < 
     lpad(regexp_substr('5.2.4', '\d+', 1, 1),10,'0') 
     ||lpad(regexp_substr('5.2.4', '\d+', 1, 2),10,'0') 
     ||lpad(regexp_substr('5.2.4', '\d+', 1, 3),10,'0') 
     ||lpad(regexp_substr('5.2.4', '\d+', 1, 4),10,'0') 
    THEN 'LESS' 
    ELSE 'NOT LESS' 
    END 
FROM dual; 

该解决方案允许每个部分;主要版本,次要版本等最多10位数字。当然,你也可以使用更大的'>'运算符进行比较。