Normalize unicode string in SQL Server?

前端 未结 4 1785
心在旅途
心在旅途 2021-01-05 02:48

Is there a function in SQL Server to normalize a unicode string? e.g.

UPDATE Orders SET Notes = NormalizeString(Notes, \'FormC\')

Unicode

4条回答
  •  -上瘾入骨i
    2021-01-05 03:05

    try this CLR function

    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using Microsoft.SqlServer.Server;
    
    public partial class UserDefinedFunctions
    {
        [Microsoft.SqlServer.Server.SqlFunction]
        public static SqlString fnRemoveDiacritics(string text)
        {
            string stringFormD = text.Normalize(System.Text.NormalizationForm.FormD);
            System.Text.StringBuilder retVal = new System.Text.StringBuilder();
            for (int index = 0; index < stringFormD.Length; index++)
            {
                if (System.Globalization.CharUnicodeInfo.GetUnicodeCategory(stringFormD[index]) != System.Globalization.UnicodeCategory.NonSpacingMark)
                    retVal.Append(stringFormD[index]);
            }
            return retVal.ToString().Normalize(System.Text.NormalizationForm.FormC);
        }
    }
    

    and in SQL

    SELECT dbo.fnRemoveDiacritics('Äěščřžýáíé')
    -- Returns: Aescrzyaie
    

    thanks to http://www.dotnetportal.cz/blogy/4/Tomas-Jecha/663/NET-Tip-6-Ciste-odstraneni-diakritiky

提交回复
热议问题