I have sample data that looks like the following:
fname | lname |
---|---|
x abc | jkn |
test | test gth |
yg-txs | gb@y |
Need to write simple query to
- remove all special character and spaces,
- concat fname and lname
- put it in fullname column, whereas values contained in this field must be without spaces and special characters.
Need optimized query because we have millions of data.
"Expected Output":
fname | lname | fullname |
---|---|---|
x abc | jkn | xabcjkn |
test | test gth | testtestgth |
yg-txs | gb@y | ygtxsgby |
I have tried below it didn’t work:
REGEXP_REPLACE({column}, '[^0-9a-zA-Z ]', '')
(REPLACE(CONCAT(fname,lname), '[$!&*_;:@#+'=%^,<.>/?|~-]', '')
2
Answers
You just need to remove the space on the
REGEX
rest all looks goodSchema (MySQL v8.0)
Query #1
As already pointed in the comments section, your issue is related to the space existing inside your regex. Additionally, employing the
REGEXP_REPLACE
only once may give you a margin of efficiency improvement, if your requirement is not having a space between first and last name.Output:
Check the demo here.
Note: If you can’t use the
REGEXP_REPLACE
command, you should ask for permissions to your provider. This is relevant as long as you’re working on millions of data and require an optimized solution.