Skip to content

Instantly share code, notes, and snippets.

@elwinar
Created September 16, 2014 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elwinar/68edabd6c5ec842d25dc to your computer and use it in GitHub Desktop.
Save elwinar/68edabd6c5ec842d25dc to your computer and use it in GitHub Desktop.
MySQL Utils
DROP FUNCTION IF EXISTS `translit`;
DELIMITER //
CREATE FUNCTION `translit`(`str` TEXT)
RETURNS text
LANGUAGE SQL
DETERMINISTIC
NO SQL
SQL SECURITY INVOKER
COMMENT ''
BEGIN
DECLARE len INTEGER;
DECLARE i INTEGER;
DECLARE dict_from VARCHAR(1024);
DECLARE dict_to VARCHAR(1024);
SET dict_from = 'ŠšŽžÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝŸÞàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿþƒ';
SET dict_to = 'SsZzAAAAAAACEEEEIIIINOOOOOOUUUUYYBaaaaaaaceeeeiiiinoooooouuuuyybf';
IF dict_to IS NOT NULL AND (CHAR_LENGTH(dict_from) != CHAR_LENGTH(dict_to)) THEN
SET @error = CONCAT('Length of dicts does not match.');
SIGNAL SQLSTATE '49999'
SET MESSAGE_TEXT = @error;
END IF;
SET len = CHAR_LENGTH(dict_from);
SET i = 1;
WHILE len >= i DO
SET @f = SUBSTR(dict_from, i, 1);
SET @t = IF(dict_to IS NULL, '', SUBSTR(dict_to, i, 1));
SET str = REPLACE(str, @f, @t);
SET i = i + 1;
END WHILE;
RETURN str;
END
//
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment