0

How can we use regular expressions in select queries? For example:-

select regex(column1, regular_expression) from table1

I found REGEXP being used in where clause but cannot find something for selects like above.

For example, phone_number column in database has (845) 545 5545, the select query should return 8455455545

3
  • SELECT * from table1 WHERE column1 REGEXP 'regular_expression'? Commented Apr 7, 2020 at 14:57
  • Could you give us an example of what are you trying to do? Commented Apr 7, 2020 at 14:58
  • To be very specific I have phone numbers in different formats in phone_number column but would like to select only numbers out of it. For example: phone_number column has (845) 545 5545, the select query should return 8455455545 Commented Apr 7, 2020 at 15:04

2 Answers 2

1

You need MySQL 8.0 for this:

mysql> select regexp_replace('(845) 545 5545', '[() ]', '') as phonenumber;
+-------------+
| phonenumber |
+-------------+
| 8455455545  |
+-------------+
Sign up to request clarification or add additional context in comments.

1 Comment

And MariaDB 10.0.
0

Instead of regex you can try replace

SELECT Replace(Replace(Replace(column1,')',''),'(',''),' ','') FROM table1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.