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;
postgresql函数实现获取中文姓名的首字母
最新推荐文章于 2024-10-08 17:39:57 发布