Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove accents on a string?

Tags:

sql-server

I have the following string

áéíóú 

which I need to convert it to

aeiou 

How can I achieve it? (I don't need to compare, I need the new string to save)

like image 586
BrunoLM Avatar asked Aug 26 '10 19:08

BrunoLM


People also ask

How do I remove the accent from a string in Python?

We can remove accents from the string by using a Python module called Unidecode. This module consists of a method that takes a Unicode object or string and returns a string without ascents.

How do you change an accented character to a regular character?

replace(/[^a-z0-9]/gi,'') . However a more intuitive solution (at least for the user) would be to replace accented characters with their "plain" equivalent, e.g. turn á , á into a , and ç into c , etc.

How do I change the accent of a character in Java?

accentsTidy = function(s){ var r=s. toLowerCase(); r = r. replace(new RegExp(/\s/g),""); r = r. replace(new RegExp(/[àáâãäå]/g),"a"); r = r.


2 Answers

Try using COLLATE:

select 'áéíóú' collate SQL_Latin1_General_Cp1251_CS_AS 

For Unicode data, try the following:

select cast(N'áéíóú' as varchar(max)) collate SQL_Latin1_General_Cp1251_CS_AS 

I am not sure what you may lose in the translation when using the second approach.

Update

It looks like œ is a special case, and we have to handle upper and lower case separately. You can do it like this (this code is a good candidate for a user-defined function):

declare @str nvarchar(max) = N'ñaàeéêèioô; Œuf un œuf' select cast(     replace((         replace(@str collate Latin1_General_CS_AS, 'Œ' collate Latin1_General_CS_AS, 'OE' collate Latin1_General_CS_AS)      ) collate Latin1_General_CS_AS, 'œ' collate Latin1_General_CS_AS, 'oe' collate Latin1_General_CS_AS) as varchar(max) ) collate SQL_Latin1_General_Cp1251_CS_AS  -- Output: -- naaeeeeioo; Oeuf un oeuf 

User Defined Function

create function dbo.fnRemoveAccents(@str nvarchar(max))   returns varchar(max) as begin return cast(     replace((         replace(@str collate Latin1_General_CS_AS, 'Œ' collate Latin1_General_CS_AS, 'OE' collate Latin1_General_CS_AS)      ) collate Latin1_General_CS_AS, 'œ' collate Latin1_General_CS_AS, 'oe' collate Latin1_General_CS_AS) as varchar(max) ) collate SQL_Latin1_General_Cp1251_CS_AS  end 
like image 173
D'Arcy Rittich Avatar answered Sep 25 '22 00:09

D'Arcy Rittich


Sometimes, the string can have another COLLATION, so you still have accents in the result. In that case, you can use this line (based on this solution here):

SELECT convert(varchar, your_string) COLLATE SQL_Latin1_General_Cp1251_CS_AS; 
like image 44
jpmottin Avatar answered Sep 24 '22 00:09

jpmottin