gotchasqlMinor
Why does REGEXP_REPLACE concatenate with a previous result?
Viewed 0 times
resultwhypreviouswithconcatenateregexp_replacedoes
Problem
This looks like strange behaviour, and I get the same on the MySQL command line as via db<>fiddle:
| foo |
| :----------------------- |
| the quick brown fox |
| jumped over the lazy dog |
| regexp_replace(foo, '[^a-z]+', '-') |
| :------------------------------------------ |
| the-quick-brown-fox |
| the-quick-brown-foxjumped-over-the-lazy-dog |
db<>fiddle here
I'd expect to see:
| REGEXP_REPLACE(foo, '[^a-z]+', '-') |
| :---------------------------------- |
| the-quick-brown-fox |
| jumped-over-the-lazy-dog |
Am I doing something wrong or is this a MySQL bug?
create table t(foo varchar(255));insert into t (foo) values
('the quick brown fox'),
('jumped over the lazy dog');select * from t;| foo |
| :----------------------- |
| the quick brown fox |
| jumped over the lazy dog |
select regexp_replace(foo, '[^a-z]+', '-') from t;| regexp_replace(foo, '[^a-z]+', '-') |
| :------------------------------------------ |
| the-quick-brown-fox |
| the-quick-brown-foxjumped-over-the-lazy-dog |
db<>fiddle here
I'd expect to see:
select REGEXP_REPLACE(foo, '[^a-z]+', '-') from t;| REGEXP_REPLACE(foo, '[^a-z]+', '-') |
| :---------------------------------- |
| the-quick-brown-fox |
| jumped-over-the-lazy-dog |
Am I doing something wrong or is this a MySQL bug?
Solution
It's a known bug, fixed in
REGEXP_RELACE() results from one result set row could carry forward to
the next row, resulting in accumulation of previous results in the
current row.
8.0.12.REGEXP_RELACE() results from one result set row could carry forward to
the next row, resulting in accumulation of previous results in the
current row.
Context
StackExchange Database Administrators Q#212125, answer score: 5
Revisions (0)
No revisions yet.