0

I have those strings in y table:

abcdefg_1056-DF or
123erttzz-1292 or
gdfgdfg_1056

What I want is only the first part, abcdefg for example for the first string. So I can replace all numbers and all -DF with an empty string, but I dont know how.

Ideas?

EOB
  • 2,975
  • 17
  • 43
  • 70

2 Answers2

1

I would suggest a regex replace. Have a look here - I think this might help you:

How to do a regular expression replace in MySQL?

Community
  • 1
  • 1
Michi Werner
  • 316
  • 3
  • 6
1

If you prever a lazy and ugly method (and not recommended for very, very much rows), you can spare yourself a user defined function like in mwerner's answer and do it simply like this:

select 
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(asdf, '0', ''), '1', ''), '2', ''), '3', ''), '4', ''), '5', ''), '6', ''), '7', ''), '8', ''), '9', ''), '_', ''), '-DF', '')
from
(
select
'abcdefg_1056-DF' as asdf
union select
'123erttzz-1292'
union select
'gdfgdfg_1056'
)q
fancyPants
  • 50,732
  • 33
  • 89
  • 96