Home > OS >  Are there any alternatives for MySQL's regex_replace feature pre-8.0 that doesn't require
Are there any alternatives for MySQL's regex_replace feature pre-8.0 that doesn't require

Time:08-12

I'm attempting to search records by passing in a squashed version of a column - If I pass in "Martin" I would expect the matching "M-@art*in" would be returned. This is something that REGEX_REPLACE seems to handle well in MySQL 8.0 and I've seen many recommendations for User Defined Functions (UDFs) that can do the same job pre-8.0. My supervisor doesn't want to rely on UDFs, and so I am wondering if there are any other options?

Any help is greatly appreciated.

CodePudding user response:

Use something like DBD::mysql, extract the column and update them.

Or, come to think of it, set your collation to UTF8

CodePudding user response:

If you have a specific set of characters allowed, you could do e.g. replace(replace(replace('M-@art*in','-',''),'@',''),'*','')

If not, you can have a somewhat cumbersome query that looks at each character individually and tests if it should be included in a group_concat that reassembles them:

select id, group_concat(substr(bar,i,substr(bar,i,1) regexp '[a-z]') order by i separator '') clean_bar
from (select 1 i union select 2 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10) i
join foo on i <= length(bar)
group by foo.id

fiddle

But you'd probably want to run that on the data and store the squashed version in a separate column if you are trying to search on it.

Note that mysql 5.7 will no longer be supported in 14 months; do plan to upgrade.

  • Related