MySQL REGEXP - Removing white space and non-numeric characters

前端 未结 4 1455
谎友^
谎友^ 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:13

    i did a function that returns all numbers only.. then i called the function by

    SELECT person FROM people WHERE returnNumericOnly(phone_number) = '123456789'
    

    my function is like this:

    DELIMITER $$
    
    USE [tablename]$$
    
    DROP FUNCTION IF EXISTS `returnNumericOnly`$$
    
    CREATE DEFINER=`root`@`localhost` FUNCTION `returnNumericOnly`(str VARCHAR(1000)) RETURNS VARCHAR(1000) CHARSET latin1
    DETERMINISTIC
    BEGIN
      DECLARE counter INT DEFAULT 0;
      DECLARE strLength INT DEFAULT 0;
      DECLARE strChar VARCHAR(1000) DEFAULT '' ;
      DECLARE retVal VARCHAR(1000) DEFAULT '';
    
      SET strLength = LENGTH(str);
    
      WHILE strLength > 0 DO
        SET counter = counter+1;
        SET strChar = SUBSTRING(str,counter,1);
        IF strChar REGEXP('[0-9]+') = 1
          THEN SET retVal = CONCAT(retVal,strChar);
        END IF;
        SET strLength = strLength -1;
        SET strChar = NULL;
      END WHILE;
    RETURN retVal;
    END $$
    
    DELIMITER ;
    
    0 讨论(0)
  • 2021-01-06 04:14

    I can think of two ways to do it, neither as elegant as I'd hoped though.

    First: Use regex in RLIKE to allow spaces and brackets everywhere:

    SELECT person 
     FROM people
     WHERE phone_number RLIKE '[() ]*1[() ]*2[() ]*3[() ]*4[() ]*5[() ]*6[() ]*7[() ]*8[() ]*9'
    

    It basically says there can be brackets and spaces in between each number. It's a bit awkward to make the string though. If you are calling mySQL from another language (say php) you could use the other language to turn '123456789' into 'x1x2x3x4x5x6x7x8x9' where 'x' is [() ]*.

    Second: use mysql REPLACE to remove spaces and brackets before doing comparison:

    SELECT person 
     FROM people
     WHERE REPLACE(REPLACE(REPLACE(phone_number,' ',''),'(',''),')','') = '123456789';
    

    This successively replaces spaces, open brackets, and closing brackets in phone_number with ''.

    I think I prefer this second method. To remove all spaces you just need REPLACE(phone_number,' ','') and to remove all () you need two REPLACE.

    I suppose the first is more flexible with respect to adding extra characters though. It's a shame MySQL doesn't have a regex-enabled REPLACE!

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-06 04:24

    try to work around like this. It's not good but it will give you what you want.

    SELECT temp.person, p.phone_number
      FROM people p
     INNER JOIN (SELECT REPLACE(REPLACE(REPLACE(phone_number,' ',''),'(',''),')','') phone_number,id
                  FROM test) temp
       ON p.id=temp.id -- primary key of the table
     WHERE temp.phone_number='123456789';
    
    0 讨论(0)
提交回复
热议问题