Search code examples
mysqlcharacter-encodinguser-defined-functions

mysql stored function return value charset


I have mysql user defined function

DELIMITER $$ 
    CREATE FUNCTION `myfunc`(`str` TEXT) RETURNS TEXT CHARSET utf8
NO SQL
DETERMINISTIC
SQL SECURITY INVOKER
BEGIN

RETURN str;
END$$

DELIMITER ;

it just returns passed parameter back without any changes

My problem is that if I pass some russian or greece letters then I get back only ?????? as response

SELECT myfunc('ывфафывавы');

will return ??????????

if I call SELECT myfunc('sdfasfsdf'); I get sdfasfsdf as result

Can't find where the prob is , any ideas ?


Solution

  • Try declaring your parameter with CHARSET utf8 just as you define your result.

    DELIMITER $$ 
    CREATE FUNCTION `myfunc`(`str` TEXT CHARSET utf8) 
        RETURNS TEXT CHARSET utf8
        NO SQL
        DETERMINISTIC
        SQL SECURITY INVOKER
        BEGIN
    
            RETURN str;
        END$$
    
    DELIMITER ;
    

    Without that MySQL casts your incoming parameter to your default charset.