DELIMITER $$ CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` FUNCTION `vn`.`testNIF`(vNIF VARCHAR(9)) RETURNS tinyint(1) DETERMINISTIC BEGIN DECLARE vIndice INT; DECLARE vParteNumerica DECIMAL(8,0); DECLARE vLetra VARCHAR(1); DECLARE vLetrasPosibles VARCHAR(24) DEFAULT "TRWAGMYFPDXBNJZSQVHLCKE"; SET vLetra = SUBSTRING(vNIF, 9, 1); SET vParteNumerica = CAST(SUBSTRING(vNIF, 1, 8) AS DECIMAL(8,0)); SET vIndice = Round(vParteNumerica MOD 23, 0) + 1; If SUBSTRING(vLetrasPosibles, vIndice, 1) = vLetra Then RETURN TRUE; ELSE RETURN FALSE; END IF; END$$ DELIMITER ;