mysqldude

Stored function example

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
MAX(version) compares as text: 2.1.9 > 2.1.10 (original)
original post
✓✓✓✓✓✓✓✓✓
pversion() pads each part to two digits (original)
original post
✓✓✓✓✓✓✓✓✓
No function: ORDER BY each part cast to a number (modern)
modern rewrite
✓✓✓✓✓✓✓✓✓
INET_ATON(CONCAT(version, '.0')) trick for parts below 256
modern rewrite
✓✓✓✓✓✓✓✓✓
MAX(version) compares as text: 2.1.9 > 2.1.10 (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
pversion() pads each part to two digits (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
No function: ORDER BY each part cast to a number (modern) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
INET_ATON(CONCAT(version, '.0')) trick for parts below 256 modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

The original article

First published 2010-09-23. The text below is the original; the table above shows how it behaves on current versions.

I have been asked once how to get the max version number which is stored in the format: xx.yy.zz. However, the information stored did not include leading zeros. This caused a problem as follows:

mysql> select version from software;
+--------+
| version   |
+--------+
| 2.1.9  |
| 2.1.10 |
+--------+
2 rows in set (0.00 sec)
mysql> select max(version) from software;
+-----------+
| max(version) |
+-----------+
| 2.1.9     |
+-----------+
1 row in set (0.00 sec)

In the example above, 2.1.9 has a higher value than the 2.1.10 which clearly is the higher of the two.

This can only be solved using a stored function as follows:

DROP FUNCTION IF EXISTS pversion;
delimiter $$
CREATE FUNCTION pversion(p_ver VARCHAR(20))
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN
	DECLARE l_start INT;
	DECLARE l_end INT;
	DECLARE l_maj VARCHAR(5);
	DECLARE l_med VARCHAR(5);
	DECLARE l_min VARCHAR(5);

	SET l_start = 1;
	SET l_end = LOCATE('.', p_ver, l_start);
	IF (l_start + 1) = l_end THEN
		SET l_maj = CONCAT('0',SUBSTR(p_ver, l_start, 1));
	ELSE
		SET l_maj = SUBSTR(p_ver, l_start, 2);
	END IF;
	SET l_start = l_end+1;
	SET l_end = LOCATE('.', p_ver, l_start);
	IF (l_start + 1) = l_end THEN
		SET l_med = CONCAT('0',SUBSTR(p_ver, l_start, 1));
	ELSE
		SET l_med = SUBSTR(p_ver, l_start, 2);
	END IF;
	SET l_start = l_end+1;
	SET l_end = LOCATE('.', p_ver, l_start);
	IF LENGTH(SUBSTR(p_ver, l_start)) = 1 THEN
		SET l_min = CONCAT('0',SUBSTR(p_ver, l_start));
	ELSE
		SET l_min = SUBSTR(p_ver, l_start);
	END IF;
	RETURN CONCAT(l_maj,'.',l_med,'.',l_min);

END;
$$
delimiter ;

Now if we attempt to access the information using this function:

mysql> select max(pversion(version)) from software;
+---------------------+
| max(pversion(version)) |
+---------------------+
| 02.01.10            |
+---------------------+
1 row in set (0.00 sec)