Last active
March 19, 2025 13:06
-
-
Save jgdoncel/bc20b39b8cd612c4a26dfcaf3bb14dd8 to your computer and use it in GitHub Desktop.
MySQL Function to remove accents and special characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP FUNCTION IF EXISTS fn_remove_accents; | |
DELIMITER | | |
CREATE FUNCTION fn_remove_accents( textvalue VARCHAR(10000) ) RETURNS VARCHAR(10000) | |
BEGIN | |
SET @textvalue = textvalue COLLATE utf8_general_ci;; | |
-- ACCENTS | |
SET @withaccents = 'ŠšŽžÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝŸÞàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿþƒ'; | |
SET @withoutaccents = 'SsZzAAAAAAACEEEEIIIINOOOOOOUUUUYYBaaaaaaaceeeeiiiinoooooouuuuyybf'; | |
SET @count = LENGTH(@withaccents); | |
WHILE @count > 0 DO | |
SET @textvalue = REPLACE(@textvalue, SUBSTRING(@withaccents, @count, 1), SUBSTRING(@withoutaccents, @count, 1)); | |
SET @count = @count - 1; | |
END WHILE; | |
-- SPECIAL CHARS | |
SET @special = '«»’”“!@#$%¨&()_+=§¹²³£¢¬"`´{[^~}]<,>.:;?/°ºª+|\'''; | |
SET @count = LENGTH(@special); | |
WHILE @count > 0 do | |
SET @textvalue = REPLACE(@textvalue, SUBSTRING(@special, @count, 1), ''); | |
SET @count = @count - 1; | |
END WHILE; | |
RETURN @textvalue; | |
END | |
| | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use in Azure Data Studio the next (only for symbols):
DROP FUNCTION IF EXISTS REMOVE_SYMBOLS;
CREATE FUNCTION REMOVE_SYMBOLS( text_value TEXT(100) )
RETURNS TEXT(100) DETERMINISTIC
BEGIN
SET @text_value = text_value COLLATE utf8_general_ci;
-- SPECIAL CHARS
-- Use the escape character \ before the single quote'
SET @special = '«»’”“!@#$%¨&()_+=§¹²³£¢¬"`´{[^~}]<,>.:;?/°ºª#+.-*|'';
SET @count = LENGTH(@special);
END