postgresql函数实现获取中文姓名的首字母

CREATE OR REPLACE FUNCTION GET_FIRST_PINYIN_CHAR(s character varying)
  RETURNS character varying AS
$BODY$
declare
  retval character varying;
  c character varying;
  b bytea;  
  w integer;
begin
retval='';
  c=left(s,1);
  b=convert_to(c,'GB18030')::bytea;
  if get_byte(b,0)<127 then
    retval= upper(c);
  elsif length(b)=2 then
    begin
    w=get_byte(b,0)*256+get_byte(b,1);
    if w between 48119 and 49061 then  
      retval= 'J';
    elsif w between 54481 and 55289 then  
      retval= 'Z';
    elsif w between 53689 and 54480 then  
      retval= 'Y';
    elsif w between 51446 and 52208 then  
      retval= 'S';
    elsif w between 52980 and 53640 then  
      retval= 'X';
    elsif w between 49324 and 49895 then  
      retval= 'L';
    elsif w between 45761 and 46317 then 
      retval= 'C';
    elsif w between 45253 and 45760 then  
      retval= 'B';
    elsif w between 46318 and 46825 then  
      retval= 'D';
    elsif w between 47614 and 48118 then  
      retval= 'H';
    elsif w between 50906 and 51386 then  
      retval= 'Q';
    elsif w between 52218 and 52697 then  
      retval= 'T';
    elsif w between 49896 and 50370 then 
      retval= 'M';
    elsif w between 47297 and 47613 then  
      retval= 'G';
    elsif w between 47010 and 47296 then
      retval= 'F';
    elsif w between 50622 and 50905 then 
      retval= 'P';
    elsif w between 52698 and 52979 then 
      retval= 'W';
    elsif w between 49062 and 49323 then 
      retval= 'K';
    elsif w between 50371 and 50613 then  
      retval= 'N';
    elsif w between 46826 and 47009 then 
      retval= 'E';
    elsif w between 51387 and 51445 then
      retval= 'R';
    elsif w between 45217 and 45252 then 
      retval= 'A';
    elsif w between 50614 and 50621 then  
      retval= 'O';
    end if;
    end;
  end if;
return retval;
end;
$BODY$
  LANGUAGE plpgsql IMMUTABLE;
  


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值