mysqldude

Stored function example

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
getProduct() scans back to the last digit (original)
original post
✓✓✓✓✓✓✓✓✓
REGEXP_SUBSTR(product, '^.*[0-9]') with no function (MySQL 8.0+, MariaDB 10.0.5+)
modern rewrite
✗ 1305✗ 1305✓✓✓✓✓✓✓
getProduct() scans back to the last digit (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
REGEXP_SUBSTR(product, '^.*[0-9]') with no function (MySQL 8.0+, MariaDB 10.0.5+) modern rewrite
MySQL 5.6✗ 13055.7✗ 13058.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
What the warnings and failures say (1)
  • REGEXP_SUBSTR(product, '^.*[0-9]') with no function (MySQL 8.0+, MariaDB 10.0.5+) (MySQL 5.6, MySQL 5.7): error 1305: FUNCTION t_1.REGEXP_SUBSTR does not exist

The original article

First published 2011-03-01. The text below is the original; the table above shows how it behaves on current versions.

Today I responded to someone that had a problem with sorting their data. Unfortunately their data model was the problem more than the data. However, I thought I would share with you the problem and the resulting code that I generated for them.

The issue is that an apparel system included a field that contained both the apparel model name and a particular style. For instance, jeans01white. The format of the data always included the model name, a number and finally the style. However, the model name may include numbers. As a result, the person was looking for a way of getting only the model from this.

The algorithm is simple, start at the end and continue scanning each character until a number is found. Then take the substring from the first character until and including this first numeric. This is not possible (or complex) in a single query. As a result a stored function can help with this processing:

--
-- Ronan Cashell (c) IT Integrated Business Solutions 2011
-- 01 March 2011
-- https://it-iss.com
--
DROP FUNCTION IF EXISTS getProduct;

DELIMITER $$
CREATE FUNCTION getProduct(aProduct VARCHAR(30))
RETURNS VARCHAR(30)
NO SQL
BEGIN
	DECLARE count INT;
	DECLARE i INT;
	DECLARE prod VARCHAR(30);
	SET prod = aProduct;
	SET count = LENGTH(aProduct);
	SET i = 0;
	WHILE i < count AND (SUBSTRING(prod, count-i, 1) < '0' OR SUBSTRING(prod, count-i, 1) > '9') DO
		SET i = i + 1;
	END WHILE;
	RETURN LEFT(prod, count-i);
END;
$$

DELIMITER ;

In action a table contains the following product numbers and the resulting output using this function:

mysql> select getProduct(product) as model, product from c;+-----------------------+--------------------------+
| model                 | product                  |
+-----------------------+--------------------------+
| A01                   | A01black                 |
| A01                   | A01white                 |
| A02                   | A02yellow                |
| A03                   | A03blue                  |
| A03                   | A03grey                  |
| A09                   | A09green                 |
| clothing-dress-y-5255 | clothing-dress-y-5255red |
+-----------------------+--------------------------+
7 rows in set (0.01 sec)