Monday, 12 January 2015

Detecting and validating email address in weblogs/tweets - MySQL example function

We regularly dump data onto Mysql from different sources. And the hardest part is to cleanse and filter out the desirable inforamation.

One of the key uses of the below function in MySql is to extract valid email addresses and reject the invalid ones.

MySql Example function:

DELIMITER $$
CREATE FUNCTION fn_email_address_test(column_value Varchar(200)) RETURNS tinyint(1)
    READS SQL DATA
    DETERMINISTIC
BEGIN
DECLARE is_valid_email BOOLEAN; 

SET is_valid_email = 0; 


IF column_value NOT REGEXP '^[A-Za-z0-9._%-]+@[A-Za-z0-9]+\.[A-Za-z]+[A-Za-z]{2}$'
 then
SET is_valid_email = 0;
else 
set is_valid_email = 1;
END IF;
RETURN is_valid_email;
END$$
DELIMITER ;

The above function will successfully identify 98% email addresses.
Please let us know if the above post helped. 

Give us your valuable feedback in the comment section.


No comments:

Post a Comment

Please share your thoughts and let us know the topics you want covered