PostgreSQL全角、半角互相轉換
標簽
PostgreSQL , 全角 , 半角 , 轉換 , ascii , chr , 編碼
背景
轉載原文
我們知道,客戶許多時候錄入數據時,並不注意輸入法是否是全角模式,然後就造成輸入的內容,有的是全角有的是半角,造成我們做分析統計時的不便,為此,編寫相應的函數。當然這個過程應盡量在前台完成,但如果針對已經存在的數據,下麵提供的兩個函數就比較方便了。
1、全角轉換為半角函數
--© 2014,swish,原版首發:https://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,原版首發:https://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編碼從33126 (十六進製 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)
參考
https://www.biaodianfu.com/python-convert-between-unicode-fullwidth-halfwidth-characters.html
最後更新:2017-05-04 20:32:19