Mysql – Regex replace with empty string results in NULL characters in between

MySQLmysql-8.0regex

Replacing a character with regexp_replace results in NUL characters in between.

SELECT REGEXP_REPLACE(aColumn,'c', '')
INTO OUTFILE 'Replaced.csv';

for abcd results in:

ab d (infact: abNULd)

This does not occur with just REPLACE

using MySQL 8

Best Answer

Ugh! The plot thickens:

mysql> SELECT REGEXP_REPLACE('abcd', 'c', '') = 'abd';
+-----------------------------------------+
| REGEXP_REPLACE('abcd', 'c', '') = 'abd' |
+-----------------------------------------+
|                                       1 |
+-----------------------------------------+

mysql> SELECT HEX(REGEXP_REPLACE('abcd', 'c', '')), HEX('abd');
+--------------------------------------+------------+
| HEX(REGEXP_REPLACE('abcd', 'c', '')) | HEX('abd') |
+--------------------------------------+------------+
| 610062006400                         | 616264     |
+--------------------------------------+------------+

mysql> SELECT LENGTH(REGEXP_REPLACE('abcd', 'c', '')), LENGTH('abd');
+-----------------------------------------+---------------+
| LENGTH(REGEXP_REPLACE('abcd', 'c', '')) | LENGTH('abd') |
+-----------------------------------------+---------------+
|                                       6 |             3 |
+-----------------------------------------+---------------+

mysql> SELECT @@version;
+-----------+
| @@version |
+-----------+
| 8.0.15    |
+-----------+

Looks like there are NULs all over! And looks like they are ignored--somewhat!

I filed http://bugs.mysql.com/94203

MariaDB 10.3.11 "does the right thing" ( 1 / no NULs / 3,3 )