• Oracle中实现中文字符串的首字母简拼转换


    /**
     *实现中文字符串的首字母简拼转换
     *@in i_str nvarchar2 要转换的字符串
     *@return result varchar2 返回值
     */
    create or replace function CHS2Short(i_str in nvarchar2) return varchar2 is
      c1       varchar2(5);
      c2       varchar2(2);
      p        number(6);
      n1       integer;
      n2       integer;
      result   varchar2(32767);
    begin

    p:=length(i_str);
    --dbms_output.put_line(i_str||':'||p);
    for i in 1..p
    loop
    c2:=substr(i_str,i,1);
    n1:=floor(ascii(c2)/256);
    n2:=mod(ascii(c2),256);
    if n1=0 and n2<=129 then c1:=c2;
    elsif n2<>127 and n2<>255 and not (n2 between 0 and 63)
    then
    select  case
    when c2>='丂' and c2<'芭'then 'A' 
    when c2>='芭' and c2<'擦'then 'B'
    when c2>='擦' and c2<'搭'then 'C'
    when c2>='搭' and c2<'蛾'then 'D'
    when c2>='蛾' and c2<'发'then 'E'
    when c2>='发' and c2<'噶'then 'F'
    when c2>='噶' and c2<'哈'then 'G'
    when c2>='哈' and c2<'击'then 'H'
    when c2>='击' and c2<'喀'then 'J'
    when c2>='喀' and c2<'垃'then 'K'
    when c2>='垃' and c2<'妈'then 'L'
    when c2>='妈' and c2<'拿'then 'M'
    when c2>='拿' and c2<'哦'then 'N'
    when c2>='哦' and c2<'啪'then 'O'
    when c2>='啪' and c2<'期'then 'P'
    when c2>='期' and c2<'然'then 'Q'
    when c2>='然' and c2<'撒'then 'R'
    when c2>='撒' and c2<'塌'then 'S'
    when c2>='塌' and c2<'挖'then 'T'
    when c2>='挖' and c2<'稀'then 'W'
    when c2>='稀' and c2<'压'then 'X'
    when c2>='压' and c2<'匝'then 'Y'
    when c2>='匝' and c2<='鼱' then 'Z'
    end
    into c1
    from dual;
    else
    c1:='error';
    end if;
    result:=result||c1;
    end loop;
    dbms_output.put_line(i_str||':'||result);
    return result;
    end CHS2Short;

    测试:

    select CHS2Short('中国') as r from dual ;

    结果:

    ZG

    测试OK!

  • 相关阅读:
    静静的看twittervision
    MSSQL中的随机函数
    紧张非封闭式开发中
    be my friend
    这个五一
    检讨
    SilverLight,有多少人关心呢?
    并查集模板题P3367 【模板】并查集
    并查集简单介绍
    约数之和模板题
  • 原文地址:https://www.cnblogs.com/yuanermen/p/2087596.html
Copyright © 2020-2023  润新知