暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL全角、半角互相转换

digoal 2017-05-04
959

作者

digoal

日期

2017-05-04

标签

PostgreSQL , 全角 , 半角 , 转换 , ascii , chr , 编码


背景

转载原文

http://blog.qdac.cc/?p=1289

我们知道,客户许多时候录入数据时,并不注意输入法是否是全角模式,然后就造成输入的内容,有的是全角有的是半角,造成我们做分析统计时的不便,为此,编写相应的函数。当然这个过程应尽量在前台完成,但如果针对已经存在的数据,下面提供的两个函数就比较方便了。

1、全角转换为半角函数

```
--© 2014,swish,原版首发:http://blog.qdac.cc/?p=1281,自由使用,保留版权

create or replace function CnFullToHalf(s character varying)
returns character varying
as
$$
declare
retval character varying;
c character varying;
l integer;
begin
l=length(s);
retval='';
while l>0 loop
c=left(s,1);
if c=' ' then -- 全角空格
retval=retval||' ';
elsif c>='!' and c<='~' then -- 全角!到~的范围
retval=retval || chr(ascii('!')+ascii(c)-ascii('!'));
else
retval=retval||c;
end if;
s=substring(s,2,l-1);
l=l-1;
end loop;
return retval;
end;
$$language plpgsql strict immutable;
```

2、半角转全角

```
--© 2014,swish,原版首发:http://blog.qdac.cc/?p=1281,自由使用,保留版权

create or replace function CnHalfToFull(s character varying)
returns character varying
as
$$
declare
retval character varying;
c integer;
l integer;
begin
l=length(s);
retval='';
while l>0 loop
c=ascii(left(s,1));
if c=32 then -- 空格
retval=retval||' ';
elsif c>=33 and c<=126 then
retval=retval || chr(ascii('!')+c-ascii('!'));
else
retval=retval||chr(c);
end if;
s=substring(s,2,l-1);
l=l-1;
end loop;
return retval;
end;
$$language plpgsql strict immutable;
```

适合UTF8字符集。

例子

```
test01=# select CnHalfToFull('123ab?-.*&^%#@~+=-:;/><|\"y');
cnhalftofull


123ab?-_.*&^%#@~+_=-:;/><|\\"y
(1 row)

test01=# select CnFullToHalf('123ab?-_.*&^%#@~+_=-:;/><|\\"y');
cnfulltohalf


123ab?-.*&^%#@~+=-:;/><|\"y
(1 row)
```

python 全角半角转换

全角半角字符的unicode编码关系

全角字符unicode编码从65281~65374 (十六进制 0xFF01 ~ 0xFF5E)

半角字符unicode编码从33~126 (十六进制 0x21~ 0x7E)

空格比较特殊,全角为 12288(0x3000),半角为 32(0x20)

除空格外,全角/半角按unicode编码排序在顺序上是对应的(半角 + 65248 = 全角)

所以可以直接通过用+-法来处理非空格数据,对空格单独处理。

用到的一些函数

chr()函数用一个范围在range(256)内的(就是0~255)整数作参数,返回一个对应的字符。

unichr()跟它一样,只不过返回的是Unicode字符。

ord()函数是chr()函数或unichr()函数的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的ASCII数值,或者Unicode数值。

PG全角半角转换函数

根据以上规则,可以写成全角半角转换函数

全角转半角

create or replace function full_to_half(text) returns text as $$ select string_agg(col, '') from ( select case when ascii(col) >= 65281 and ascii(col) <=65374 then chr(ascii(col)-65248) when ascii(col) = 12288 then chr(32) else col end AS col from (select regexp_split_to_table($1, '') as col) t ) tt; $$ language sql strict immutable;

半角转全角

create or replace function half_to_full(text) returns text as $$ select string_agg(col, '') from ( select case when ascii(col) >= 33 and ascii(col) <=126 then chr(ascii(col)+65248) when ascii(col) = 32 then chr(12288) else col end AS col from (select regexp_split_to_table($1, '') as col) t ) tt; $$ language sql strict immutable;

例子

```
test01=# select full_to_half('123ab?-_.*&^%#@~+_=-:;/><|\\"y!');
full_to_half


123ab?-.*&^%#@~+=-:;/><|\"y!
(1 row)

test01=# select half_to_full('123ab?-.*&^%#@~+=-:;/><|\"y! 123ab?-_.*&^%#@~+_=-:;/><|\\"y!');
half_to_full


123ab?-_.*&^%#@~+_=-:;/><|\\"y!   123ab?-_.*&^%#@~+_=-:;/><|\\"y!
(1 row)
```

参考

http://blog.qdac.cc/?p=1289

https://www.biaodianfu.com/python-convert-between-unicode-fullwidth-halfwidth-characters.html

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论