1
BEGIN
2
-- variables
3
DECLARE delimLength INT(11) DEFAULT 1;
4
DECLARE v1Length INT(11) DEFAULT 0;
5
DECLARE v2Length INT(11) DEFAULT 0;
6
DECLARE Result INT(11) DEFAULT 0;
7
-- set delimiter length
8
SET delimLength = LENGTH(delim);
9
-- find the length of array indexes between each string, taking into account length of the delimiter
10
SET v1Length = ((LENGTH(ver1) - LENGTH(REPLACE(ver1, delim, ''))) / delimLength)+1;
11
SET v2Length = ((LENGTH(ver2) - LENGTH(REPLACE(ver2, delim, ''))) / delimLength)+1;
12
SET @x = 1;
13
REPEAT
14
-- set result based on current array index; alpha is ignored
15
IF CONVERT(REPLACE(SUBSTRING(SUBSTRING_INDEX(ver1, delim, @x), LENGTH(SUBSTRING_INDEX(ver1, delim, @x-1))+1), delim, ''), UNSIGNED INTEGER)
16
> CONVERT(REPLACE(SUBSTRING(SUBSTRING_INDEX(ver2, delim, @x), LENGTH(SUBSTRING_INDEX(ver2, delim, @x-1))+1), delim, ''), UNSIGNED INTEGER)
17
THEN SET Result = -1;
18
END IF;
19
IF CONVERT(REPLACE(SUBSTRING(SUBSTRING_INDEX(ver1, delim, @x), LENGTH(SUBSTRING_INDEX(ver1, delim, @x-1))+1), delim, ''), UNSIGNED INTEGER)
20
< CONVERT(REPLACE(SUBSTRING(SUBSTRING_INDEX(ver2, delim, @x), LENGTH(SUBSTRING_INDEX(ver2, delim, @x-1))+1), delim, ''), UNSIGNED INTEGER)
21
THEN SET Result = 1;
22
END IF;
23
SET @x = @x + 1;
24
-- Do this until a result is discovered, or the shortest array is completed
25
UNTIL Result <> 0 OR @x > v1Length OR @x > v2Length END REPEAT;
26
-- if arrays at shortest indexes are equal, then longest index wins
27
IF Result = 0 THEN
28
SET Result = IF(v1Length > v2Length, -1, IF(v1Length < v2Length, 1, 0));
29
END IF;
30
-- return result
31
RETURN Result;
32
END