简体   繁体   English

如何将 SQL 查询中的返回值从西里尔文转换为拉丁文

[英]How convert return value in SQL query from cyrillic to latin

I have a table where I store cars registration numbers, but some registration numbers are in Cyrillic.我有一张表,用于存储汽车注册号,但有些注册号是西里尔字母。 If I search registration numbers "XX0000XX" where 'X' is in Latin and in table one or more 'X' is in Cyrillic finds nothing.如果我搜索注册号“XX0000XX”,其中 'X' 是拉丁文,而表中的一个或多个 'X' 是西里尔文,什么也找不到。 Is there a way to write this query, for example:有没有办法编写这个查询,例如:

Select from cars where reg_num = 'XX0000XX'

in such a way that it can also find the records that contain both Cyrillic and Latin alphabet?以这样的方式它也可以找到包含西里尔字母和拉丁字母的记录? In where clause string is 100% Latin alphabetwhere子句中,字符串是 100% 拉丁字母

If you use MS SQL.如果您使用 MS SQL。 You can use the transliteration function您可以使用音译功能

    GO
/****** Object:  UserDefinedFunction [dbo].[TransLit]    Script Date: 05.04.2017 10:25:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
ALTER FUNCTION [dbo].[TransLit]
(
 @@String VarChar(max)
)
RETURNS VarChar(max)
AS
BEGIN
DECLARE @TransTable TABLE(
   Rus Char 
  ,Lat VarChar(2)
 )INSERT @TransTable SELECT 'А','A'
  UNION ALL SELECT 'Б','B'
  UNION ALL SELECT 'В','V'
  UNION ALL SELECT 'Г','G'
  UNION ALL SELECT 'Д','D'
  UNION ALL SELECT 'Е','E'
  UNION ALL SELECT 'Ё','YO'
  UNION ALL SELECT 'Ж','ZH'
  UNION ALL SELECT 'З','Z'
  UNION ALL SELECT 'И','I'
  UNION ALL SELECT 'Й','Y'
  UNION ALL SELECT 'К','K'
  UNION ALL SELECT 'Л','L'
  UNION ALL SELECT 'М','M'
  UNION ALL SELECT 'Н','N'
  UNION ALL SELECT 'О','O'
  UNION ALL SELECT 'П','P'
  UNION ALL SELECT 'Р','R'
  UNION ALL SELECT 'С','S'
  UNION ALL SELECT 'Т','T'
  UNION ALL SELECT 'У','U'
  UNION ALL SELECT 'Ф','F'
  UNION ALL SELECT 'Х','H'
  UNION ALL SELECT 'Ц','C'
  UNION ALL SELECT 'Ч','CH'
  UNION ALL SELECT 'Ш','SH'
  UNION ALL SELECT 'Щ','SH'
  UNION ALL SELECT 'Ъ',''''
  UNION ALL SELECT 'Ы','Y'
  UNION ALL SELECT 'Ь',''''
  UNION ALL SELECT 'Э','E'
  UNION ALL SELECT 'Ю','YU'
  UNION ALL SELECT 'Я','YA'

 DECLARE @Result VarChar(max)
 SET @Result = @@String
 SELECT @Result = Replace(@Result,Upper(Rus) COLLATE Cyrillic_General_CS_AS,Upper(Lat)) FROM @TransTable WHERE @@String LIKE '%' + Rus + '%'
 SELECT @Result = Replace(@Result,Lower(Rus) COLLATE Cyrillic_General_CI_AS,Lower(Lat)) FROM @TransTable WHERE @@String LIKE '%' + Rus + '%'
 RETURN @Result
END

Then your script然后你的脚本

Select * from cars where TransLit(reg_num) = 'XX0000XX'

In 'XX0000XX' use latin.在“XX0000XX”中使用拉丁语。

Here is s T-SQL solution of this problem. 是此问题的 T-SQL 解决方案。 Main idea is that you have to replace all russian С, Х, Н and so on with latin C, X, H. Here are all similar char pairs which were used it this function.主要思想是你必须用拉丁语 C、X、H 替换所有俄语 С、Х、Н 等。这里有所有类似的字符对,它们都用于这个函数。 You can write your own function that replaces first char of each pair with second.您可以编写自己的函数,用第二个字符替换每对字符的第一个字符。

Ѐ -> E
Ё -> E
Ѕ -> S
І -> I
Ї -> I
Ј -> J
Њ -> H
Ќ -> K
А -> A
В -> B
Е -> E
К -> K
М -> M
Н -> H
О -> O
Р -> P
С -> C
Т -> T
У -> Y
Х -> X
Ь -> b
а -> a
е -> e
к -> k
м -> m
о -> o
р -> p
с -> c
т -> t
у -> y
х -> x
ь -> b
ѐ -> e
ё -> e
ѕ -> s
і -> i
ї -> i
ј -> j
ћ -> h
ќ -> k
ѡ -> w
Ҏ -> P
ҏ -> p
Қ -> K
қ -> k
Ҝ -> K
ҝ -> k
Ҟ -> K
ҟ -> k
Ҡ -> K
ҡ -> k
Ң -> H
Ҥ -> H
Ҫ -> C
ҫ -> c
Ҭ -> T
ҭ -> t
Ү -> Y
ү -> y
Ұ -> Y
ұ -> y
Ҳ -> X
ҳ -> x
Һ -> h
һ -> h
Ҽ -> E
ҽ -> e
Ҿ -> E
ҿ -> e
Ӏ -> I
Ӄ -> K
ӄ -> k
Ӈ -> H
ӈ -> H
Ӊ -> H
ӊ -> H
Ӎ -> M
ӎ -> m
ӏ -> I
Ӑ -> A
ӑ -> a
Ӓ -> A
ӓ -> a
Ӗ -> E
ӗ -> e
Ӧ -> O
ӧ -> o
Ӯ -> Y
ӯ -> y
Ӱ -> Y
ӱ -> y
Ӳ -> Y
ӳ -> y
Ӽ -> X
ӽ -> x

Here is a solution converting Cyrillic to Latin for SQL Server.这是 SQL Server 将西里尔文转换为拉丁文的解决方案。

create function fn_Cyrillic2Latin (@string nvarchar(max)) 
returns nvarchar(max) as
begin

set @string = replace (@string, N'ый'       ,N'y')
set @string = replace (@string, N'ЫЙ'       ,N'Y')
set @string = replace (@string, N'а'        ,N'a')
set @string = replace (@string, N'б'        ,N'b')
set @string = replace (@string, N'в'        ,N'v')
set @string = replace (@string, N'г'        ,N'g')
set @string = replace (@string, N'д'        ,N'd')
set @string = replace (@string, N'е'        ,N'e')
set @string = replace (@string, N'ё'        ,N'yo')
set @string = replace (@string, N'ж'        ,N'zh')
set @string = replace (@string, N'з'        ,N'z')
set @string = replace (@string, N'и'        ,N'i')
set @string = replace (@string, N'й'        ,N'y')
set @string = replace (@string, N'к'        ,N'k')
set @string = replace (@string, N'л'        ,N'l')
set @string = replace (@string, N'м'        ,N'm')
set @string = replace (@string, N'н'        ,N'n')
set @string = replace (@string, N'о'        ,N'o')
set @string = replace (@string, N'п'        ,N'p')
set @string = replace (@string, N'р'        ,N'r')
set @string = replace (@string, N'с'        ,N's')
set @string = replace (@string, N'т'        ,N't')
set @string = replace (@string, N'у'        ,N'u')
set @string = replace (@string, N'ф'        ,N'f')
set @string = replace (@string, N'х'        ,N'kh')
set @string = replace (@string, N'ц'        ,N'c')
set @string = replace (@string, N'ч'        ,N'ch')
set @string = replace (@string, N'ш'        ,N'sh')
set @string = replace (@string, N'щ'        ,N'shch')
set @string = replace (@string, N'ъ'        ,N' ')
set @string = replace (@string, N'ы'        ,N'y')
set @string = replace (@string, N'ь'        ,N'')
set @string = replace (@string, N'э'        ,N'e')
set @string = replace (@string, N'ю'        ,N'yu')
set @string = replace (@string, N'я'        ,N'ya')
set @string = replace (@string, N'А'        ,N'A')
set @string = replace (@string, N'Б'        ,N'B')
set @string = replace (@string, N'В'        ,N'V')
set @string = replace (@string, N'Г'        ,N'G')
set @string = replace (@string, N'Д'        ,N'D')
set @string = replace (@string, N'Е'        ,N'E')
set @string = replace (@string, N'Ё'        ,N'YO')
set @string = replace (@string, N'Ж'        ,N'ZH')
set @string = replace (@string, N'З'        ,N'Z')
set @string = replace (@string, N'И'        ,N'I')
set @string = replace (@string, N'Й'        ,N'Y')
set @string = replace (@string, N'К'        ,N'K')
set @string = replace (@string, N'Л'        ,N'L')
set @string = replace (@string, N'М'        ,N'M')
set @string = replace (@string, N'Н'        ,N'N')
set @string = replace (@string, N'О'        ,N'O')
set @string = replace (@string, N'П'        ,N'P')
set @string = replace (@string, N'Р'        ,N'R')
set @string = replace (@string, N'С'        ,N'S')
set @string = replace (@string, N'Т'        ,N'T')
set @string = replace (@string, N'У'        ,N'U')
set @string = replace (@string, N'Ф'        ,N'F')
set @string = replace (@string, N'Х'        ,N'KH')
set @string = replace (@string, N'Ц'        ,N'C')
set @string = replace (@string, N'Ч'        ,N'CH')
set @string = replace (@string, N'Ш'        ,N'SH')
set @string = replace (@string, N'Щ'        ,N'SHCH')
set @string = replace (@string, N'Ъ'        ,N'')
set @string = replace (@string, N'Ы'        ,N'Y')
set @string = replace (@string, N'Ь'        ,N'')
set @string = replace (@string, N'Э'        ,N'E')
set @string = replace (@string, N'Ю'        ,N'YU')
set @string = replace (@string, N'Я'        ,N'YA')

return @String
end

Macedonian Cyrillic can be coverted to Latin using this function使用此函数可以将马其顿西里尔文转换为拉丁文

CREATE FUNCTION dbo.MacedonianCyrillic2Latin (@String varchar(MAX))
RETURNS nvarchar(MAX)
AS 
BEGIN
    DECLARE @s nvarchar(MAX) = @String

    SET @s = REPLACE(@s, N'А', N'A')
    SET @s = REPLACE(@s, N'Б', N'B')
    SET @s = REPLACE(@s, N'В', N'V')
    SET @s = REPLACE(@s, N'Г', N'Đ')
    SET @s = REPLACE(@s, N'Д', N'D')
    SET @s = REPLACE(@s, N'Ѓ', N'G')
    SET @s = REPLACE(@s, N'Е', N'E')
    SET @s = REPLACE(@s, N'Ж', N'Ž')
    SET @s = REPLACE(@s, N'З', N'Z')
    SET @s = REPLACE(@s, N'Ѕ', N'Dz')
    SET @s = REPLACE(@s, N'И', N'I')
    SET @s = REPLACE(@s, N'Ј', N'J')
    SET @s = REPLACE(@s, N'К', N'K')
    SET @s = REPLACE(@s, N'Л', N'L')
    SET @s = REPLACE(@s, N'Љ', N'Lj')
    SET @s = REPLACE(@s, N'М', N'M')
    SET @s = REPLACE(@s, N'Н', N'N')
    SET @s = REPLACE(@s, N'Њ', N'Nj')
    SET @s = REPLACE(@s, N'О', N'O')
    SET @s = REPLACE(@s, N'П', N'P')
    SET @s = REPLACE(@s, N'Р', N'R')
    SET @s = REPLACE(@s, N'С', N'S')
    SET @s = REPLACE(@s, N'Т', N'T')
    SET @s = REPLACE(@s, N'Ќ', N'Kj')
    SET @s = REPLACE(@s, N'У', N'U')
    SET @s = REPLACE(@s, N'Ф', N'G')
    SET @s = REPLACE(@s, N'Х', N'H')
    SET @s = REPLACE(@s, N'Ц', N'C')
    SET @s = REPLACE(@s, N'Ч', N'Č')
    SET @s = REPLACE(@s, N'Џ', N'Dž')
    SET @s = REPLACE(@s, N'Ш', N'Š')
    SET @s = REPLACE(@s, N'а', N'a')
    SET @s = REPLACE(@s, N'б', N'b')
    SET @s = REPLACE(@s, N'в', N'v')
    SET @s = REPLACE(@s, N'г', N'đ')
    SET @s = REPLACE(@s, N'д', N'd')
    SET @s = REPLACE(@s, N'ѓ', N'g')
    SET @s = REPLACE(@s, N'е', N'e')
    SET @s = REPLACE(@s, N'ж', N'ž')
    SET @s = REPLACE(@s, N'з', N'z')
    SET @s = REPLACE(@s, N'ѕ', N'dz')
    SET @s = REPLACE(@s, N'и', N'i')
    SET @s = REPLACE(@s, N'ј', N'j')
    SET @s = REPLACE(@s, N'к', N'k')
    SET @s = REPLACE(@s, N'л', N'l')
    SET @s = REPLACE(@s, N'љ', N'lj')
    SET @s = REPLACE(@s, N'м', N'm')
    SET @s = REPLACE(@s, N'н', N'n')
    SET @s = REPLACE(@s, N'њ', N'nj')
    SET @s = REPLACE(@s, N'о', N'o')
    SET @s = REPLACE(@s, N'п', N'p')
    SET @s = REPLACE(@s, N'р', N'r')
    SET @s = REPLACE(@s, N'с', N's')
    SET @s = REPLACE(@s, N'т', N't')
    SET @s = REPLACE(@s, N'ќ', N'kj')
    SET @s = REPLACE(@s, N'у', N'u')
    SET @s = REPLACE(@s, N'ф', N'g')
    SET @s = REPLACE(@s, N'х', N'h')
    SET @s = REPLACE(@s, N'ц', N'c')
    SET @s = REPLACE(@s, N'ч', N'č')
    SET @s = REPLACE(@s, N'џ', N'dž')
    SET @s = REPLACE(@s, N'ш', N'š')

    RETURN @s 
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM