Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL REGEXP - Removing white space and non-numeric characters

Tags:

mysql

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 '123456789'

It wont find:

(123) 456 789
(12) 456789
123 456 789
etc etc

In my MySQL query is it possible to strip everything and just leave the numbers?

I'd also like to search removing all spaces and just leaving the numbers and characters and also removing all characters and just leaving numbers and spaces. Not sure how easy all this is.

Appreciate your help!

like image 475
Ben Sinclair Avatar asked Jan 13 '12 01:01

Ben Sinclair


3 Answers

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.

like image 158
davogotland Avatar answered Nov 16 '22 08:11

davogotland


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 ;
like image 33
lenne Avatar answered Nov 16 '22 10:11

lenne


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!

like image 4
mathematical.coffee Avatar answered Nov 16 '22 10:11

mathematical.coffee