I\'m looking for a sql statement to count the number of unique characters in a string.
e.g.
3333333333 -> returns 1
1113333333 -> returns 2
111
DROP FUNCTION IF EXISTS test.count_chrs;
CREATE DEFINER=`test`@`localhost` FUNCTION `count_chrs`(s CHAR(100)) RETURNS CHAR(4)
BEGIN
DECLARE string_length int(4);
DECLARE unique_string CHAR(100) DEFAULT "";
DECLARE count_unique int(4) DEFAULT 0;
DECLARE current_char int(4) DEFAULT 1;
SET string_length = CHAR_LENGTH(s);
WHILE current_char <= string_length DO
IF (!LOCATE(SUBSTR(s, current_char, 1), unique_string)) THEN
SET count_unique = count_unique + 1;
SET unique_string = CONCAT(unique_string, SUBSTR(s, current_char, 1));
END IF;
SET current_char = current_char + 1;
END WHILE;
RETURN count_unique;
END;
I'm very newbie with MySQL functions declaration, but this could turn you to the right way.