####################################
##  Nur Zahlen auslesen FUNCTION  ##
####################################
SET GLOBAL log_bin_trust_function_creators=1;
DROP FUNCTION IF EXISTS digits;
DELIMITER |
CREATE FUNCTION digits( str CHAR(32) ) RETURNS CHAR(32)
BEGIN
  DECLARE i, len SMALLINT DEFAULT 1;
  DECLARE ret CHAR(32) DEFAULT '';
  DECLARE c CHAR(1);
 
  IF str IS NULL
  THEN 
    RETURN "";
  END IF;
 
  SET len = CHAR_LENGTH( str );
  REPEAT
    BEGIN
      SET c = MID( str, i, 1 );
      IF c BETWEEN '0' AND '9' THEN 
        SET ret=CONCAT(ret,c);
      END IF;
      SET i = i + 1;
    END;
  UNTIL i > len END REPEAT;
  RETURN ret;
END |
DELIMITER ;
 
##############
## Beispiel ##
##############
 
SELECT *,digits(CONCAT(area_code,NUMBER)) AS FullNumber FROM phone 
WHERE area_code <> "-" 
AND area_code IS NOT NULL 
AND area_code <> ""
LIMIT 50;