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
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 ;