2017-09-19 4 views
2

나는 자동차 등록 번호가 저장된 테이블을 갖고 있는데, 이지만 일부 등록 번호는 키릴 문자로되어 있습니다. "X"가 라틴어와 테이블에있는 "XX0000XX"라는 등록 번호를 검색하면 키릴 자모에 'X'가 하나 이상 표시됩니다. 이 쿼리를 작성하는 방법은 예를 들어, 거기에 : 그것은 또한 모두 키릴 문자와 라틴 문자를 포함하는 레코드를 찾을 수있는 방법에 키릴 문자에서 라틴 문자로 SQL 쿼리의 반환 값을 변환하는 방법

Select from cars where reg_num = 'XX0000XX' 

? where에서 절 문자열은 100 % 라틴 알파벳

+0

어떤 데이터베이스 (예 : MySQL, SQL Server)를 사용하고 있습니까? –

+0

나는 그것을 사용하고있다 MySQL – Radoslav

+0

'WHERE' 절의 문자열은 이미 두 알파벳에 모두 들어 있지 않습니까? 여기서 장기적인 해결책은 모든 데이터를 포함하는 데이터 정렬을 선택하는 것입니다. –

답변

0

MS SQL을 사용하는 경우. 당신은 라틴어 사용 'XX0000XX'에서 음역 기능

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' 

를 사용할 수 있습니다.

+0

MySQL을 위해 시도해보십시오. https://pavuk.me/mysql-transliterate-function/ –

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 
0

다음은 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 
관련 문제