Pretty bad, but I think this will do what you need.
NOTE. Assuming ASCII and you count only az, AZ.
DROP FUNCTION IF EXISTS numberOfLetters; DELIMITER // CREATE FUNCTION numberOfLetters (inStr CHAR(255)) RETURNS INT BEGIN DECLARE strLen INT; DECLARE letterCt INT; DECLARE pos INT; DECLARE curLetter CHAR(1); SET pos := 0; SET letterCt := 0; SELECT LENGTH(inStr) INTO strLen; ctLoop: LOOP IF (strLen = 0) THEN LEAVE ctLoop; END IF; SELECT SUBSTR(inStr, strLen, 1) INTO curLetter; IF (ASCII(curLetter) >= 65 AND ASCII(curLetter) <= 90) OR (ASCII(curLetter) >= 97 AND ASCII(curLetter) <= 122) THEN SET letterCt := letterCt + 1; END IF; SET strLen := strLen - 1; END LOOP ctLoop; RETURN letterCt; END // DELIMITER ; select numberOfLetters('abc123 def');
Output:
+-------------------------------+ | numberOfLetters('abc123 def') | +-------------------------------+ | 6 | +-------------------------------+
source share