
-- Updates the bad formated phones

UPDATE vn2008.Clientes
	SET telefono = pbx.phone_format (telefono),
		fax = pbx.phone_format (fax),
		movil = pbx.phone_format (movil);

UPDATE vn2008.Consignatarios
	SET telefono = pbx.phone_format (telefono),
		movil = pbx.phone_format (movil);

UPDATE vn2008.Contactos
	SET telefono = pbx.phone_format (telefono),
		fax = pbx.phone_format (fax),
		movil = pbx.phone_format (movil);

-- Indexes the phone columns

ALTER TABLE vn2008.`Clientes` ADD INDEX(`Telefono`);
ALTER TABLE vn2008.`Clientes` ADD INDEX(`movil`);

ALTER TABLE vn2008.`Consignatarios` ADD INDEX(`telefono`);
ALTER TABLE vn2008.`Consignatarios` ADD INDEX(`movil`);

ALTER TABLE vn2008.`Contactos` ADD INDEX(`Telefono`);
ALTER TABLE vn2008.`Contactos` ADD INDEX(`Movil`);

-- Returns all bad formated phones

SELECT telefono, movil c FROM vn2008.Clientes
	WHERE telefono NOT REGEXP '^[0-9]+$'
		OR movil NOT REGEXP '^[0-9]+$'
UNION
SELECT telefono, movil FROM vn2008.Consignatarios
	WHERE telefono NOT REGEXP '^[0-9]+$'
		OR movil NOT REGEXP '^[0-9]+$'
UNION
SELECT telefono, movil FROM vn2008.Contactos
	WHERE telefono NOT REGEXP '^[0-9]+$'
		OR movil NOT REGEXP '^[0-9]+$';

