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

时间:2017-09-19 11:21:50

标签: mysql sql

我有一张表存放汽车注册号, 但有些注册号是西里尔文。如果我搜索注册号“XX0000XX”,其中'X'是拉丁语,而在表格中,'X'在西里尔语中找不到任何内容。 有没有办法编写此查询,例如:

filename2
filename3
filename10
file name11

以这种方式,它也可以找到包含西里尔字母和拉丁字母的记录?在Select from cars where reg_num = 'XX0000XX' 子句中,字符串是100%拉丁字母

4 个答案:

答案 0 :(得分:0)

如果您使用MS SQL。您可以使用音译功能

    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

然后你的脚本

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

在'XX0000XX'中使用拉丁语。

答案 1 :(得分:0)

Here是此问题的T-SQL解决方案。主要的想法是你必须用拉丁语C,X,H替换所有俄语С,Х,Н等。这里有所有类似的字符对,它们都使用了这个功能。您可以编写自己的函数,用第二个替换每对中的第一个字符。

Ѐ -> 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

答案 2 :(得分:0)

这是一个将Cyrillic转换为拉丁语为SQL Server的解决方案。

JsonObject

答案 3 :(得分:0)

使用此功能可以将马其顿西里尔文转换为拉丁文

1