How can I remove accents on a string?

Sql Server

Sql Server Problem Overview


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)

Sql Server Solutions


Solution 1 - Sql Server

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

Solution 2 - Sql Server

Use the translate function:

SELECT TRANSLATE(
N'INPUT: ïÜ×ÌùµŪč©īĐÃÙěÓńÿâŘåòÔÕłćýçÀŻūìóèůüíÄûØõäÕťżîŃà£êřßøŽÖáďÉęúÂĪāËžŮōÑÇĆź®Š¥ĘĒśŹĚŚšŸ¢ŁéąÈđÆÍÛĄÝĎēČÊŌŇöÏňëÎæãŤñÒÚĀÅÁô',
N'ÁÀÂÃÄÅàáâãäåĀāąĄæÆÇçćĆčČ¢©đĐďĎÈÉÊËèéêëěĚĒēęĘÌÍÎÏìíîïĪīłŁ£ÑñňŇńŃÒÓÔÕÕÖØòóôõöøŌōřŘ®ŠšśŚßťŤÙÚÛÜùúûüůŮŪūµ×¥ŸÿýÝŽžżŻźŹ', 
N'aaaaaaaaaaaaaaaaaaccccccccddddeeeeeeeeeeeeeeiiiiiiiiiilllnnnnnooooooooooooooooorrsssssttuuuuuuuuuuuuuxyyyyyzzzzzz');

-- OUTPUT: 'INPUT: iuxiuuuccidaueonyaraooolcycazuioeuuiauooaotzioaleosozoadeeuaiaezuoncczrsyeeszessycleaedaiuaydeceonoineiaatnouaaao'

Check this link to find more 'look-a-like' characters:

https://github.com/apache/lucene-solr/blob/1ca7067a810578d4e246b5434b9cdcec7145d230/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/ASCIIFoldingFilter.java#L189

Solution 3 - Sql Server

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;

Solution 4 - Sql Server

I had the same problem. In Greek for proper conversion to UPPER() you must suppress accent. Changing collation caused issues in other applications. Putting some REPLACE() functions I had more control on the behavior maintaining collation. Below is my ToUpperCaseGR function.

	SET ANSI_NULLS ON
	GO
	SET QUOTED_IDENTIFIER ON
	GO

	create FUNCTION ToUpperCaseGR
	(
	 @word nvarchar(max)
	)
	RETURNS nvarchar(max)
	AS
	BEGIN
		-- Declare the return variable here
		declare @res nvarchar(max)
		set @res = UPPER(@word)
		set @res = replace(@res,'Ά','Α')
		set @res = replace(@res,'Έ','Ε')
		set @res = replace(@res,'Ί','Ι')
		set @res = replace(@res,'Ή','Η')
		set @res = replace(@res,'Ό','Ο')
		set @res = replace(@res,'Ύ','Υ')
		set @res = replace(@res,'Ώ','Ω')


		-- Return the result of the function
		RETURN @res

	END
	GO

Solution 5 - Sql Server

Use this function:

CREATE FUNCTION [dbo].[F_RemoveDiacritics] (
 @String varchar(max)
)   RETURNS varchar(max)

AS BEGIN
DECLARE @StringResult VARCHAR(max);

select @StringResult= @String collate SQL_Latin1_General_Cp1251_CS_AS

return @StringResult


END

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrunoLMView Question on Stackoverflow
Solution 1 - Sql ServerD'Arcy RittichView Answer on Stackoverflow
Solution 2 - Sql ServerYolofyView Answer on Stackoverflow
Solution 3 - Sql ServerjpmottinView Answer on Stackoverflow
Solution 4 - Sql ServerΘάνος ΔογάνηςView Answer on Stackoverflow
Solution 5 - Sql ServerEsperento57View Answer on Stackoverflow