【问题标题】:How convert return value in SQL query from cyrillic to latin如何将 SQL 查询中的返回值从西里尔字母转换为拉丁字母
【发布时间】:2017-09-19 11:21:50
【问题描述】:

我有一张存储汽车登记号的表格, 但有些注册号是西里尔文。如果我搜索注册号“XX0000XX”,其中“X”是拉丁文,而表中的一个或多个“X”是西里尔文,则什么也找不到。 有没有办法写这个查询,例如:

Select from cars where reg_num = 'XX0000XX'

以这样一种方式,它也可以找到同时包含西里尔字母和拉丁字母的记录?在where 子句中,字符串是 100% 拉丁字母

【问题讨论】:

  • 您使用的是哪个数据库(例如 MySQL、SQL Server)?
  • 我用的是 MySQL
  • WHERE 子句中的字符串不是已经出现在两个字母表中了吗?这里的长期解决方案是选择一个涵盖所有数据的排序规则。

标签: mysql sql


【解决方案1】:

如果您使用 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”中使用拉丁文。

【讨论】:

    【解决方案2】:

    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
    

    【讨论】:

      【解决方案3】:

      这是一个将西里尔文转换为拉丁文用于 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
      

      【讨论】:

        【解决方案4】:

        马其顿西里尔文可以使用此函数转换为拉丁文

        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
        

        【讨论】:

          猜你喜欢
          • 2017-06-19
          • 1970-01-01
          • 2012-06-09
          • 1970-01-01
          • 1970-01-01
          • 2017-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多