ORACLE函数获取汉字拼音首字母

ORACLE函数获取汉字拼音首字母

参考文章
http://news.newhua.com/news1/program_database/2008/526/0852615338H3AACHF553F9C4218GD3DCAIDB6GABC0B96052J6026AG.html?lt=common

列表一行汉字
WITH A AS
(SELECT '获取汉字拼音首字母' W FROM DUAL)
SELECT SUBSTR(W, ROWNUM, 1) FROM A
CONNECT BY ROWNUM <= (SELECT LENGTH(W) FROM A);
返回结果








用汉字字符集对这个列表进行排序
WITH A AS
(SELECT '获取汉字拼音首字母' W FROM DUAL)
SELECT SUBSTR(W, ROWNUM, 1) FROM A
CONNECT BY ROWNUM <= (SELECT LENGTH(W) FROM A)
ORDER BY NLSSORT(SUBSTR(W, ROWNUM, 1), 'NLS_SORT=SCHINESE_PINYIN_M');
返回结果








那么根据这个原理,上面输入一个拼音A打头的字"澳",后面输入一个B大头的字"吧",找到每个音节的起止的汉字是哪个
WITH A AS
(
SELECT ROWNUM RN, CHR(ROWNUM) C FROM DUAL CONNECT BY LEVEL <= 65535
)
SELECT * FROM A WHERE LENGTHB(C) = 2
AND RN > 32768
AND NLSSORT(C, 'NLS_SORT=SCHINESE_PINYIN_M') > NLSSORT('澳', 'NLS_SORT=SCHINESE_PINYIN_M')
AND NLSSORT(C, 'NLS_SORT=SCHINESE_PINYIN_M') < NLSSORT('吧', 'NLS_SORT=SCHINESE_PINYIN_M')
ORDER BY NLSSORT(C, 'NLS_SORT=SCHINESE_PINYIN_M');
根据返回的结果,就能看到,A的结束和B的开始的汉子分别是:“�” “八”,依次类推可以找到其他的分界点,那么最后的函数就是如下:

CREATE OR REPLACE FUNCTION F_TRANS_PINYIN_CAPITAL(P_NAME IN VARCHAR2) RETURN VARCHAR2 AS
V_COMPARE VARCHAR2(100);
V_RETURN VARCHAR2(4000);

FUNCTION F_NLSSORT(P_WORD IN VARCHAR2) RETURN VARCHAR2 AS
BEGIN
RETURN NLSSORT(P_WORD, 'NLS_SORT=SCHINESE_PINYIN_M');
END;
BEGIN
FOR I IN 1..LENGTH(P_NAME) LOOP
V_COMPARE := F_NLSSORT(SUBSTR(P_NAME, I, 1));
IF V_COMPARE >= F_NLSSORT(' 吖 ') AND V_COMPARE <= F_NLSSORT('� ') THEN
V_RETURN := V_RETURN || 'a';
ELSIF V_COMPARE >= F_NLSSORT('八 ') AND V_COMPARE <= F_NLSSORT('簿 ') THEN
V_RETURN := V_RETURN || 'b';
ELSIF V_COMPARE >= F_NLSSORT('嚓 ') AND V_COMPARE <= F_NLSSORT('�e ') THEN
V_RETURN := V_RETURN || 'c';
ELSIF V_COMPARE >= F_NLSSORT('�� ') AND V_COMPARE <= F_NLSSORT('�z ') THEN
V_RETURN := V_RETURN || 'd';
ELSIF V_COMPARE >= F_NLSSORT('�� ') AND V_COMPARE <= F_NLSSORT('�� ') THEN
V_RETURN := V_RETURN || 'e';
ELSIF V_COMPARE >= F_NLSSORT('发 ') AND V_COMPARE <= F_NLSSORT('�g ') THEN
V_RETURN := V_RETURN || 'f';
ELSIF V_COMPARE >= F_NLSSORT('旮 ') AND V_COMPARE <= F_NLSSORT('�B ') THEN
V_RETURN := V_RETURN || 'g';
ELSIF V_COMPARE >= F_NLSSORT('�o ') AND V_COMPARE <= F_NLSSORT('�� ') THEN
V_RETURN := V_RETURN || 'h';
ELSIF V_COMPARE >= F_NLSSORT('丌 ') AND V_COMPARE <= F_NLSSORT('�h ') THEN
V_RETURN := V_RETURN || 'j';
ELSIF V_COMPARE >= F_NLSSORT('咔 ') AND V_COMPARE <= F_NLSSORT('�i ') THEN
V_RETURN := V_RETURN || 'k';
ELSIF V_COMPARE >= F_NLSSORT('垃 ') AND V_COMPARE <= F_NLSSORT('�^ ') THEN
V_RETURN := V_RETURN || 'l';
ELSIF V_COMPARE >= F_NLSSORT('�` ') AND V_COMPARE <= F_NLSSORT('�� ') THEN
V_RETURN := V_RETURN || 'm';
ELSIF V_COMPARE >= F_NLSSORT('�� ') AND V_COMPARE <= F_NLSSORT('�� ') THEN
V_RETURN := V_RETURN || 'n';
ELSIF V_COMPARE >= F_NLSSORT('�p ') AND V_COMPARE <= F_NLSSORT('�a ') THEN
V_RETURN := V_RETURN || 'o';
ELSIF V_COMPARE >= F_NLSSORT('�r ') AND V_COMPARE <= F_NLSSORT('曝 ') THEN
V_RETURN := V_RETURN || 'p';
ELSIF V_COMPARE >= F_NLSSORT('七 ') AND V_COMPARE <= F_NLSSORT('�d ') THEN
V_RETURN := V_RETURN || 'q';
ELSIF V_COMPARE >= F_NLSSORT('�� ') AND V_COMPARE <= F_NLSSORT('�U ') THEN
V_RETURN := V_RETURN || 'r';
ELSIF V_COMPARE >= F_NLSSORT('仨 ') AND V_COMPARE <= F_NLSSORT('�R ') THEN
V_RETURN := V_RETURN || 's';
ELSIF V_COMPARE >= F_NLSSORT('�@ ') AND V_COMPARE <= F_NLSSORT('�X ') THEN
V_RETURN := V_RETURN || 't';
ELSIF V_COMPARE >= F_NLSSORT('�� ') AND V_COMPARE <= F_NLSSORT('�F ') THEN
V_RETURN := V_RETURN || 'w';
ELSIF V_COMPARE >= F_NLSSORT('夕 ') AND V_COMPARE <= F_NLSSORT('�R ') THEN
V_RETURN := V_RETURN || 'x';
ELSIF V_COMPARE >= F_NLSSORT('丫 ') AND V_COMPARE <= F_NLSSORT('� ') THEN
V_RETURN := V_RETURN || 'y';
ELSIF V_COMPARE >= F_NLSSORT('�� ') AND V_COMPARE <= F_NLSSORT('�� ') THEN
V_RETURN := V_RETURN || 'z';
END IF;
END LOOP;
RETURN V_RETURN;
END;

测试一下:
SELECT F_TRANS_PINYIN_CAPITAL('罗华') FROM DUAL   
返回 lh
使用的时候这样查询:
select name from users t1 where (t1.name = ? or F_TRANS_PINYIN_CAPITAL(t1.name) = ?)
如果里面的?参数传递的是“罗华”,就是查询用户名是落花的人,如果传递的是lh的话,查询的就是拼音是lh的人

你可能感兴趣的:(ORACLE函数获取汉字拼音首字母)