Skip to content

Instantly share code, notes, and snippets.

@hr-sadooghi
Created February 27, 2017 08:09
Show Gist options
  • Save hr-sadooghi/b7c5886855d27dedd516bc717375a881 to your computer and use it in GitHub Desktop.
Save hr-sadooghi/b7c5886855d27dedd516bc717375a881 to your computer and use it in GitHub Desktop.
english digit to persian digit and reverse
DROP FUNCTION IF EXISTS fa2EnDigit;
DELIMITER $$
CREATE FUNCTION fa2EnDigit(str TEXT) RETURNS TEXT
BEGIN
DECLARE t TEXT;
SET t = str;
SET t = REPLACE(t, '۱', '1');
SET t = REPLACE(t, '۲', '2');
SET t = REPLACE(t, '۳', '3');
SET t = REPLACE(t, '۴', '4');
SET t = REPLACE(t, '۵', '5');
SET t = REPLACE(t, '۶', '6');
SET t = REPLACE(t, '۷', '7');
SET t = REPLACE(t, '۸', '8');
SET t = REPLACE(t, '۹', '9');
SET t = REPLACE(t, '۰', '0');
RETURN t;
END$$
DELIMITER ;
DROP FUNCTION IF EXISTS en2FaDigit;
DELIMITER $$
CREATE FUNCTION en2FaDigit(str TEXT) RETURNS TEXT
BEGIN
DECLARE t TEXT;
SET t = str;
SET t = REPLACE(t, '1', '۱');
SET t = REPLACE(t, '2', '۲');
SET t = REPLACE(t, '3', '۳');
SET t = REPLACE(t, '4', '۴');
SET t = REPLACE(t, '5', '۵');
SET t = REPLACE(t, '6', '۶');
SET t = REPLACE(t, '7', '۷');
SET t = REPLACE(t, '8', '۸');
SET t = REPLACE(t, '9', '۹');
SET t = REPLACE(t, '0', '۰');
RETURN t;
END$$
DELIMITER ;
-- usage
SELECT fa2EnDigit("۱۳۹۵/۱۲/۹");
SELECT en2FaDigit("1395/12/9");
SELECT id, fa2EnDigit(body)
FROM post;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment