MySQL REGEXP - Removing white space and non-numeric characters

前端 未结 4 1458
谎友^
谎友^ 2021-01-06 03:36

I want to do a search in a MySQL database for phone numbers.

At present with this query:

SELECT person FROM people WHERE phone_number RLIKE \'1234567         


        
4条回答
  •  星月不相逢
    2021-01-06 04:15

    how about:

    SELECT
        person,
        replace(replace(replace(replace(phone_number,' ',''),'(',''),')',''),'-','') as phone_number
    FROM
        people
    WHERE
        phone_number RLIKE '^[+]?[-() 0-9]+$';
    

    matches numbers that start with a plus sign, they may contain hyphens, parenthesis and spaces. but no plus signs other than at the start. and also no characters. also removes hyphens, spaces and parenthesis.

提交回复
热议问题