Convert SQL code which works fine on MYSQL to MariaDB

Hi,

In my DB, I have four columns of phone data which are poorly formatted (some entries contain text, other chars, multiple valid numbers etc.) and I am trying to extract valid phone numbers and store them in a fifth column in a clean format. The SQL query I have extracts and stores the phone numbers correctly on my MySQL (8.0) database but when I try it on a MariaDB (10.6) database it does not work.

Here’s the code I have so far:

UPDATE info -- table
SET phones = ( -- output column
	SELECT NULLIF( -- set NULL if empty result
		GROUP_CONCAT( -- combine phone numbers by comma
			DISTINCT REGEXP_REPLACE( -- get unique values
				REGEXP_SUBSTR(
					REGEXP_REPLACE(source_column, '[^0-9]', ''),  -- remove all non-numeric chars
					'1?([0-9]{10})', 1, numbers.n -- get 10-digit numbers
				),
				'^1', '' -- remove leading 1
			) ORDER BY col_pos, numbers.n SEPARATOR ',' -- preserve column order
		), ''
	)
	FROM (
		SELECT col1 AS source_column, 1 AS col_pos UNION ALL -- get values from all source columns
		SELECT col2, 2 UNION ALL
		SELECT col3, 3 UNION ALL
		SELECT col4, 4
	) AS columns
	JOIN ( -- account for multiple phones in column entries
		SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
		UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 -- this assumes a column entry will have max 10 multiple phone numbers
	) AS numbers
	ON REGEXP_SUBSTR(
		REGEXP_REPLACE(columns.source_column, '[^0-9]', ''),
		'1?([0-9]{10})', 1, numbers.n
	) IS NOT NULL
);

I will appreciate any pointers to how to edit the code in order to make it work on MariaDB. Thanks!

It would help if you tell us the error message

@Thalius is right always state the error.

My guess is the issue are REGEXP_SUBSTR and probably REGEXP_REPLACE

MySQL
REGEXP_SUBSTR(expr, pat[, pos[, occurrence[, match_type]]])

MariaDB
REGEXP_SUBSTR(subject,pattern)

As you can see MariaDB only take 2 parameters. You have more than 2.

If all you want is 10 digits, 2 parameters should work for both.

technically dont they want 9 or 10. They strip down the number to 10 or 11 digits (because the first 1 is ? optional), and then try to remove a leading 1 if it exists after having been stripped down.

So they’ll end up with a 10 digit number, unless the number was already 10 digits and started with a 1, in which case they’ll end up with 9.

1234567890 matches the pattern 1?([0-9]{10}) in the same way that 11234567890 does, but without the leading 1. If you then strip a leading 1…