C#、Oracle、Sql server中拼音查询的函数

来源:互联网 发布:河北广电网络集团招聘 编辑:程序博客网 时间:2024/05/21 15:47
C# sqlserver oracle 的都有 不过发现那个 C# 的好像"楠"字查的时候会有问题。。。不知道为啥。。

C#

  1/// <summary>
  2    /// 生成拼音简码
  3    /// </summary>
  4    /// <param name="unicodeString">Unicode编码字符串</param>
  5    /// <returns>拼音简码:string</returns>

  6    public static string GetPinyinCode(string unicodeString)
  7    {
  8        int i = 0;
  9        ushort key = 0;
 10        string strResult = string.Empty;
 11        //创建两个不同的encoding对象
 12        Encoding unicode = Encoding.Unicode;
 13        //创建GBK码对象
 14        Encoding gbk = Encoding.GetEncoding(936);
 15        //将unicode字符串转换为字节
 16        byte[] unicodeBytes = unicode.GetBytes(unicodeString);
 17        //再转化为GBK码
 18        byte[] gbkBytes = Encoding.Convert(unicode, gbk, unicodeBytes);
 19        while (i < gbkBytes.Length)
 20        {
 21            //如果为数字\字母\其他ASCII符号
 22            if (gbkBytes[i] <= 127)
 23            {
 24                strResult = strResult + (char)gbkBytes[i];
 25                i++;
 26            }

 27            #region 否则生成汉字拼音简码,取拼音首字母
 28            else
 29            {
 30
 31                key = (ushort)(gbkBytes[i] * 256 + gbkBytes[i + 1]);
 32                if (key >= '\uB0A1' && key <= '\uB0C4')
 33                {
 34                    strResult = strResult + "A";
 35                }

 36                else if (key >= '\uB0C5' && key <= '\uB2C0')
 37                {
 38                    strResult = strResult + "B";
 39                }

 40                else if (key >= '\uB2C1' && key <= '\uB4ED')
 41                {
 42                    strResult = strResult + "C";
 43                }

 44                else if (key >= '\uB4EE' && key <= '\uB6E9')
 45                {
 46                    strResult = strResult + "D";
 47                }

 48                else if (key >= '\uB6EA' && key <= '\uB7A1')
 49                {
 50                    strResult = strResult + "E";
 51                }

 52                else if (key >= '\uB7A2' && key <= '\uB8C0')
 53                {
 54                    strResult = strResult + "F";
 55                }

 56                else if (key >= '\uB8C1' && key <= '\uB9FD')
 57                {
 58                    strResult = strResult + "G";
 59                }

 60                else if (key >= '\uB9FE' && key <= '\uBBF6')
 61                {
 62                    strResult = strResult + "H";
 63                }

 64                else if (key >= '\uBBF7' && key <= '\uBFA5')
 65                {
 66                    strResult = strResult + "J";
 67                }

 68                else if (key >= '\uBFA6' && key <= '\uC0AB')
 69                {
 70                    strResult = strResult + "K";
 71                }

 72                else if (key >= '\uC0AC' && key <= '\uC2E7')
 73                {
 74                    strResult = strResult + "L";
 75                }

 76                else if (key >= '\uC2E8' && key <= '\uC4C2')
 77                {
 78                    strResult = strResult + "M";
 79                }

 80                else if (key >= '\uC4C3' && key <= '\uC5B5')
 81                {
 82                    strResult = strResult + "N";
 83                }

 84                else if (key >= '\uC5B6' && key <= '\uC5BD')
 85                {
 86                    strResult = strResult + "O";
 87                }

 88                else if (key >= '\uC5BE' && key <= '\uC6D9')
 89                {
 90                    strResult = strResult + "P";
 91                }

 92                else if (key >= '\uC6DA' && key <= '\uC8BA')
 93                {
 94                    strResult = strResult + "Q";
 95                }

 96                else if (key >= '\uC8BB' && key <= '\uC8F5')
 97                {
 98                    strResult = strResult + "R";
 99                }

100                else if (key >= '\uC8F6' && key <= '\uCBF9')
101                {
102                    strResult = strResult + "S";
103                }

104                else if (key >= '\uCBFA' && key <= '\uCDD9')
105                {
106                    strResult = strResult + "T";
107                }

108                else if (key >= '\uCDDA' && key <= '\uCEF3')
109                {
110                    strResult = strResult + "W";
111                }

112                else if (key >= '\uCEF4' && key <= '\uD188')
113                {
114                    strResult = strResult + "X";
115                }

116                else if (key >= '\uD1B9' && key <= '\uD4D0')
117                {
118                    strResult = strResult + "Y";
119                }

120                else if (key >= '\uD4D1' && key <= '\uD7F9')
121                {
122                    strResult = strResult + "Z";
123                }

124                else
125                {
126                    strResult = strResult + "?";
127                }

128                i = i + 2;
129            }

130            #endregion

131        }
//end while 
132
133        return strResult;
134    }
 

Oracle

 1create or replace function fGetPy
 2(V_Str varchar2)
 3return varchar2 
 4as        
 5  v_strlen int;
 6  v_return varchar2(500);
 7  v_ii int    ;
 8  v_n int;
 9  v_c char(1);
10  v_chn varchar2(2);
11  V_RC varchar2(500);
12begin
13 V_RC:=V_Str;
14
15 v_strlen :=len(V_RC);
16 v_return := '';
17 v_ii:=0
18 while v_ii<v_strlen loop    
19  v_ii:=v_ii+1;
20  v_n:=63;
21  SELECT substring(V_RC,v_ii,1INTO v_chn FROM DUAL;
22
23
24  select v_n+max(rowsf) into v_n
25   from
26   select chn,ROWNUM rowsf from(   
27    select chn from (    
28     select ''  chn  from dual
29     union  select ''   from dual 
30     union all  select ''  from dual  
31     union all  select ''   from dual 
32     union all  select ''    from dual
33     union all select ''    from dual
34     union all select ''   from dual 
35     union all select ''   from dual 
36     union all select ''  from dual--because have no 'i'    
37     union all select ''   from dual 
38     union all select ''    from dual
39     union all select ''   from dual 
40     union all select ''   from dual 
41     union all select ''    from dual
42     union all select ''   from dual 
43     union all select ''   from dual 
44     union all select ''   from dual 
45     union all select ''    from dual
46     union all select ''    from dual
47     union all select ''    from dual
48     union all select ''  from dual   
49     union all select ''  from dual    
50     union all select '' from dual   
51     union all select ''  from dual  
52     union all select ''  from dual  
53     union all select ''    from dual
54     union all select v_chn from dual
55     )  a    
56   order by nlssort(chn,'NLS_SORT=SCHINESE_PINYIN_M'
57   ) c
58    )  b WHERE chn=v_chn ;
59
60   
61  v_c:=chr(v_n);
62  if chr(v_n) ='@' then--英文直接返回    
63   v_c:=v_chn    ;
64   end if;
65   
66 
67  v_return:=v_return||v_c;
68 end loop; 
69 
70 return v_return;    
71end;

sql server

 1create   function   fGetPy(@Str   varchar(500)='')   
 2  returns   varchar(500)   
 3  as   
 4  begin   
 5  declare   @strlen   int,@return   varchar(500),@ii   int   
 6  declare   @n   int,@c   char(1),@chn   nchar(1)   
 7    
 8  select   @strlen=len(@str),@return='',@ii=0   
 9  set   @ii=0   
10  while   @ii<@strlen   
11  begin   
12  select   @ii=@ii+1,@n=63,@chn=substring(@str,@ii,1)   
13  select   @n   =   @n   +1   
14  ,@c   =   case   chn   when   @chn   then   char(@n)   else   @c   end   
15  from(   
16  select   top   27   *   from   (   
17  select   chn   =   ''   
18  union   all   select   ''   
19  union   all   select   ''   
20  union   all   select   ''   
21  union   all   select   ''   
22  union   all   select   ''   
23  union   all   select   ''   
24  union   all   select   ''   
25  union   all   select   '' --because   have   no   'i'   
26  union   all   select   ''   
27  union   all   select   ''   
28  union   all   select   ''   
29  union   all   select   ''   
30  union   all   select   ''   
31  union   all   select   ''   
32  union   all   select   ''   
33  union   all   select   ''   
34  union   all   select   ''   
35  union   all   select   ''   
36  union   all   select   ''   
37  union   all   select   '' --no   'u'   
38  union   all   select   '' --no   'v'   
39  union   all   select   ''   
40  union   all   select   ''   
41  union   all   select   ''   
42  union   all   select   ''   
43  union   all   select   @chn)   as   a   
44  order   by   chn   COLLATE   Chinese_PRC_CI_AS     
45  )   as   b   
46  set   @return=@return+@c   
47  end   
48  return(@return)   
49  end
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 济南图书馆借书超过期限了怎么办 淘宝解绑支付宝支付密码不对怎么办 微博支付宝支付密码忘记了怎么办 图虫签约师通过了认证标识怎么办 签证做假工作证明资料被拒怎么办 在广州办个建设厅电工证怎么办 水利考的五大员证到有效期怎么办 额头注射玻尿酸吸收后不平怎么办 施工员证书挂靠拿不回来怎么办 森林公安未转政法编制的人员怎么办 北京的限行新政策外地车怎么办 报考二级建造师工作年限不够怎么办 郑州航院图书馆密码忘了怎么办 无锡妇幼预约挂号过号了怎么办 云南建筑八大员考试没通过怎么办 订车ax7一个月提不到车怎么办 提车时间到了却没车怎么办 一汽大众速腾气囊灯亮该怎么办呢? 幼儿园上课时候电脑上的课件怎么办 黑米紫薯红豆粥不好煮怎么办 母狗生了小狗后不吃东西没奶怎么办 狗给扑倒了主人不想负责怎么办 山东政务网个人中心账号忘了怎么办 网易博客忘记登入名和密码了怎么办 奶水不够怎么办怎样让奶水变多 和初恋分手多年又爱上初恋该怎么办 分手六年的初恋想要和我复合怎么办 不小心把手机里的视频删了怎么办 网签过了中介不配合过户怎么办 房屋被中介恶意网签了怎么办 重介质选矿块矿品位低怎么办 去泰国酒吧选小姐只会说中文怎么办 脚被窝烫伤后发炎里面有龙怎么办 脚被窝烫了发炎有龙怎么办 辣椒落花的药喷到孩子嘴巴了怎么办 香炉里的小米生虫子了怎么办 香炉里面放的小米生虫子了怎么办 肉肉上长满了白色的小虫子怎么办 多肉上面有白色的虫子怎么办 多肉植物根部长白色虫子怎么办 朋友玩期货把我钱赔了怎么办