Función SQL del latín al cirílico

I am looking for ms sql function to make translit from Latin to Cyrillic. Does anybody have completed solution? (for example, 'spasibo' -> 'спасибо')

preguntado el 10 de marzo de 12 a las 12:03

Why would you want to do that kind of thing in SQL? -

Cyrillic is not a language, it's an alphabet. There are different rules for transliterating, say, Ukrainian vs. Russian vs. non-Slavic languages from Cyrillic to Latin. I'd imagine that you'd need to know the specific language before doing transliteration from Latin back to Cyrillic. -

Olaf, I need in Russian. -

2 Respuestas

CREATE FUNCTION dbo.ToCyrillic
(
    @str Nvarchar(MAX)
)
RETURNS Nvarchar(MAX)
AS
BEGIN
    -- Declare the return variable here
    DECLARE @inputLength int
    DECLARE @i int
    DECLARE @latinSymbol nVarchar(2)
    DECLARE @cyrillicSymbol nVarchar(2)
    DECLARE @outputValue nVarchar(MAX)

    SET @outputValue=N''
    SET @inputLength=LEN(@str)
    SET @i=1

    DECLARE @TransTable table (upperCyr nvarchar(2) COLLATE Cyrillic_General_CI_AS, 
                                lowerCyr nvarchar(2) COLLATE Cyrillic_General_CI_AS, 
                                lowerLat nvarchar(2), cid int PRIMARY KEY IDENTITY(1,1))

    insert into @TransTable values (N'А', N'а', N'a')
    insert into @TransTable values (N'Б', N'б', N'b')
    insert into @TransTable values (N'В', N'в', N'v')
    insert into @TransTable values (N'Г', N'г', N'g')
    insert into @TransTable values (N'Д', N'д', N'd')
    insert into @TransTable values (N'Ђ', N'ђ', N'đ')
    insert into @TransTable values (N'Е', N'е', N'e')
    insert into @TransTable values (N'Ж', N'ж', N'ž')
    insert into @TransTable values (N'З', N'з', N'z')
    insert into @TransTable values (N'И', N'и', N'i')
    insert into @TransTable values (N'Ј', N'ј', N'j')
    insert into @TransTable values (N'К', N'к', N'k')
    insert into @TransTable values (N'Л', N'л', N'l')
    insert into @TransTable values (N'Љ', N'љ', N'lj')
    insert into @TransTable values (N'М', N'м', N'm')
    insert into @TransTable values (N'Н', N'н', N'n')
    insert into @TransTable values (N'Њ', N'њ', N'nj')
    insert into @TransTable values (N'О', N'о', N'o')
    insert into @TransTable values (N'П', N'п', N'p')
    insert into @TransTable values (N'Р', N'р', N'r')
    insert into @TransTable values (N'С', N'с', N's')
    insert into @TransTable values (N'Т', N'т', N't')
    insert into @TransTable values (N'Ћ', N'ћ', N'ć')
    insert into @TransTable values (N'У', N'у', N'u')
    insert into @TransTable values (N'Ф', N'ф', N'f')
    insert into @TransTable values (N'Х', N'х', N'h')
    insert into @TransTable values (N'Ц', N'ц', N'c')
    insert into @TransTable values (N'Ч', N'ч', N'č')
    insert into @TransTable values (N'Џ', N'џ', N'dž')
    insert into @TransTable values (N'Ш', N'ш', N'š')


    WHILE (@i<=@inputLength)
    BEGIN
        SET @latinSymbol=SUBSTRING(@str,@i,1)
        SET @cyrillicSymbol=@latinSymbol    -- If not found below, then use that char (e.g. numbers etc)

        IF ((@latinSymbol COLLATE Croatian_CS_AS)=UPPER(@latinSymbol))
        BEGIN
            SELECT TOP 1 @cyrillicSymbol=upperCyr FROM @TransTable WHERE lowerlat=lower(@latinSymbol) ORDER BY CID
        END
        ELSE
        BEGIN
            SELECT TOP 1 @cyrillicSymbol=lowerCyr FROM @TransTable WHERE lowerlat=lower(@latinSymbol) ORDER BY CID
        END
        SET @i=@i+1
        set @outputValue=@outputValue+@cyrillicSymbol
    END

    RETURN @outputValue

END

Respondido 27 Oct 14, 13:10

This is transliteration into cyrillic for Serbian alphabet. Feel free to adopt this function into any other alphabet. - Hrvoje Matić

Usage example: select transliterated=dbo.ToCyrillic('The quick brown fox jumps over the lazy dog') COLLATE Cyrillic_General_CI_AS - Hrvoje Matić

It's good function dbo.ToCyrillic, but not good enough. It's tree exceptions Љ=lj, Њ=nj, Џ=dž. This is alter function dbo.ToCyrillic with exceptions:

-- select dbo.ToCyrillic('Ljubav nJače džače')

ALTER FUNCTION [dbo].[ToCyrillic](@str Nvarchar(MAX))
    RETURNS Nvarchar(MAX)
    AS
BEGIN
-- Declare the return variable here
DECLARE @inputLength int
DECLARE @i int
DECLARE @latinSymbol nVarchar(2)
DECLARE @latinSymbol2 nVarchar(2)
set @latinSymbol2=null
DECLARE @cyrillicSymbol nVarchar(2)
DECLARE @outputValue nVarchar(MAX)
SET @outputValue=N''
SET @inputLength=LEN(@str)
SET @i=1

DECLARE @TransTable table (upperCyr nvarchar(2) COLLATE Cyrillic_General_CI_AS, 
                            lowerCyr nvarchar(2) COLLATE Cyrillic_General_CI_AS, 
                            lowerLat nvarchar(2), cid int PRIMARY KEY IDENTITY(1,1))

insert into @TransTable values (N'А', N'а', N'a')
insert into @TransTable values (N'Б', N'б', N'b')
insert into @TransTable values (N'В', N'в', N'v')
insert into @TransTable values (N'Г', N'г', N'g')
insert into @TransTable values (N'Д', N'д', N'd')
insert into @TransTable values (N'Ђ', N'ђ', N'đ')
insert into @TransTable values (N'Е', N'е', N'e')
insert into @TransTable values (N'Ж', N'ж', N'ž')
insert into @TransTable values (N'З', N'з', N'z')
insert into @TransTable values (N'И', N'и', N'i')
insert into @TransTable values (N'Ј', N'ј', N'j')
insert into @TransTable values (N'К', N'к', N'k')
insert into @TransTable values (N'Л', N'л', N'l')
insert into @TransTable values (N'Љ', N'љ', N'lj')
insert into @TransTable values (N'М', N'м', N'm')
insert into @TransTable values (N'Н', N'н', N'n')
insert into @TransTable values (N'Њ', N'њ', N'nj')
insert into @TransTable values (N'О', N'о', N'o')
insert into @TransTable values (N'П', N'п', N'p')
insert into @TransTable values (N'Р', N'р', N'r')
insert into @TransTable values (N'С', N'с', N's')
insert into @TransTable values (N'Т', N'т', N't')
insert into @TransTable values (N'Ћ', N'ћ', N'ć')
insert into @TransTable values (N'У', N'у', N'u')
insert into @TransTable values (N'Ф', N'ф', N'f')
insert into @TransTable values (N'Х', N'х', N'h')
insert into @TransTable values (N'Ц', N'ц', N'c')
insert into @TransTable values (N'Ч', N'ч', N'č')
insert into @TransTable values (N'Џ', N'џ', N'dž')
insert into @TransTable values (N'Ш', N'ш', N'šˇ')


WHILE (@i<=@inputLength)
BEGIN
    SET @latinSymbol=SUBSTRING(@str,@i,1)
    SET @cyrillicSymbol=@latinSymbol    -- If not found below, then use that char (e.g. numbers etc)

        -- exceptions Љ,Њ,Џ
if (@i+1<=@inputLength) set @latinSymbol2=SUBSTRING(@str,@i+1,1)    
if lower(@latinSymbol+@latinSymbol2) in ('lj','nj','dž')    
    begin
        if ((@latinSymbol COLLATE Croatian_CS_AS)=UPPER(@latinSymbol))
            BEGIN
            SELECT TOP 1 @cyrillicSymbol=upperCyr FROM @TransTable WHERE lowerlat=lower(@latinSymbol+@latinSymbol2) ORDER BY CID
            END
            ELSE
            BEGIN
            SELECT TOP 1 @cyrillicSymbol=lowerCyr FROM @TransTable WHERE lowerlat=lower(@latinSymbol+@latinSymbol2) ORDER BY CID
            END     
            SET @i=@i+2    
        end
    else--end exceptions
    begin
        IF ((@latinSymbol COLLATE Croatian_CS_AS)=UPPER(@latinSymbol))
        BEGIN
        SELECT TOP 1 @cyrillicSymbol=upperCyr FROM @TransTable WHERE lowerlat=lower(@latinSymbol) ORDER BY CID
        END
        ELSE
        BEGIN
        SELECT TOP 1 @cyrillicSymbol=lowerCyr FROM @TransTable WHERE lowerlat=lower(@latinSymbol) ORDER BY CID
        END
        SET @i=@i+1
    end
    set @outputValue=@outputValue+@cyrillicSymbol
END

RETURN @outputValue

END

respondido 04 nov., 16:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.