色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

DB2中創建一個獲取漢字拼音首字母的SQL函數

瀏覽:48日期:2023-11-09 16:43:28

需求

有些時候我們會有這樣的需求,要求使用字母從a至z對一組數據進行索引,如果數據的格式全部是半角的英文則很容易實現,但若是對一組中文數據進行索引則會引起一點小的麻煩,數據在錄入數據庫的時候可能并沒有指定一個索引字母,這就要求應用程序可以自動生成用于索引的信息。

一般對于中文數據的索引,采用詞組的首漢字拼音的首字母,例如:

詞組 索引字母

--- -----

熊貓 x

白暨豚 b

藏野驢 z

在DB2中并沒有提供相應的函數可以取得漢字拼音的首字母,我們可以利用數據庫針對中文字符集的排序功能創建一個這樣的函數。

工作原理

我們知道在使用中文字符集的數據庫中,當你對一列中文數據使用order by 排序時,排序的結果正是按照每行記錄第一個漢字的拼音首字母進行排列的,那么我們需要想辦法取得這個字母。

但是數據庫內部是如何做到這一點的呢?以中文字符集GBK為例,讓我們查看一下GBK字符集的內碼表,我們僅摘出一段:

0 1 2 3 4 5 6 7 8 9 A B C D E F

B040 癅 癆 癇 癈 癉 癊 癋 癎 癏 癐 癑 癒 癓 癕 癗 癘

B050 癙 癚 癛 癝 癟 癠 癡 癢 癤 癥 癦 癧 癨 癩 癪 癬

B060 癭 癮 癰 癱 癲 癳 癴 癵 癶 癷 癹 発 發 癿 皀 皁

B070 皃 皅 皉 皊 皌 皍 皏 皐 皒 皔 皕 皗 皘 皚 皛

B080 皜 皝 皞 皟 皠 皡 皢 皣 皥 皦 皧 皨 皩 皪 皫 皬

B090 皭 皯 皰 皳 皵 皶 皷 皸 皹 皺 皻 皼 皽 皾 盀 盁

B0A0 盃 啊 阿 埃 挨 哎 唉 哀 皚 癌 藹 矮 艾 礙 愛 隘

B0B0 鞍 氨 安 俺 按 暗 岸 胺 案 骯 昂 盎 凹 敖 熬 翱

B0C0 襖 傲 奧 懊 澳 芭 捌 扒 叭 吧 笆 八 疤 巴 拔 跋

B0D0 靶 把 耙 壩 霸 罷 爸 白 柏 百 擺 佰 敗 拜 稗 斑

B0E0 班 搬 扳 般 頒 板 版 扮 拌 伴 瓣 半 辦 絆 邦 幫

B0F0 梆 榜 膀 綁 棒 磅 蚌 鎊 傍 謗 苞 胞 包 褒 剝

可以看到從B0A0-1 開始,至B0C0-5,是拼音A開頭的漢字,恰好是按照拼音字母的先后順序排列,并且把音調的因素也考慮進去了,由此,可以推斷出,數據庫在GBk編碼的數據庫中對漢字進行排序,即是依照字符內碼表的編碼進行的。

我們把B0C0-5 位置的漢字記錄下來,即“澳”字,這是以“a”拼音開頭在內碼表中排列在最后的漢字,用同樣的方法,我們找出所有以拼音從b至z開頭,在內碼表中排列在最后的漢字,與26個字母的對應關系如下:

'澳' a

'怖' b

'錯' c

'墮' d

'貳' e

'咐' f

'過' g

'禍' h

i

'駿' j

'闊' k

'絡' l

'穆' m

'諾' n

'漚' o

'瀑' p

'群' q

'弱' r

'所' s

'唾' t

u

v

'誤' w

'迅' x

'孕' y

'座 z

注:沒有以'i','u','v'開頭的漢語拼音。

現在假若我們拿出任何一個漢字,放在我們挑選出的這些漢字中間,利用數據庫進行一次使用GBK字符集的排序,我們便能夠根據這個漢字排列的相對位置得到其拼音首字母。

利用sql語句生成一組上述漢字的結果集,我們將'i','u','v' 三個空缺漢字的位置補上了上一個拼音的漢字,

select t1.strChn

from ( select '澳' strChn from sysibm.sysdummy1

union all

select '怖' strChn from sysibm.sysdummy1

union all

select '錯' strChn from sysibm.sysdummy1

union all

select '墮' strChn from sysibm.sysdummy1

union all

select '貳' strChn from sysibm.sysdummy1

union all

select '咐' strChn from sysibm.sysdummy1

union all

select '過' strChn from sysibm.sysdummy1

union all

select '禍' strChn from sysibm.sysdummy1

union all

select '禍' strChn from sysibm.sysdummy1

union all

select '駿' strChn from sysibm.sysdummy1

union all

select '闊' strChn from sysibm.sysdummy1

union all

select '絡' strChn from sysibm.sysdummy1

union all

select '穆' strChn from sysibm.sysdummy1

union all

select '諾' strChn from sysibm.sysdummy1

union all

select '漚' strChn from sysibm.sysdummy1

union all

select '瀑' strChn from sysibm.sysdummy1

union all

select '群' strChn from sysibm.sysdummy1

union all

select '弱' strChn from sysibm.sysdummy1

union all

select '所' strChn from sysibm.sysdummy1

union all

select '唾' strChn from sysibm.sysdummy1

union all

select '唾' strChn from sysibm.sysdummy1

union all

select '唾' strChn from sysibm.sysdummy1

union all

select '誤' strChn from sysibm.sysdummy1

union all

select '迅' strChn from sysibm.sysdummy1

union all

select '孕' strChn from sysibm.sysdummy1

union all

select '座' strChn from sysibm.sysdummy1

) as t1

實現

接下來很方便的就可以寫出這個函數的具體實現,在實現的代碼中,我們又加入了針對英文字母的處理,函數編譯后,可通過如下方式調用:

select getIndex( '索' ) index from dual;

index

------

f

原代碼如下:

create function getIndex (

in_strChn varchar(2)

) returns char(1)

language sql

external action

reads sql data

begin atomic

declare chResult char(1);

declare n integer default 0;

if( in_strChn = '' or in_strChn is null or lengthb( in_strChn ) > 2 ) then

return null;

end if;

if(( ascii( in_strChn ) >= ascii('A') and ascii( in_strChn ) <= ascii('Z') )

or ( ascii( in_strChn ) >= ascii('a') and ascii( in_strChn ) <= ascii('z')) ) then

return lcase( substr( in_strChn, 1, 1 ) );

end if;

for myloop as

select t2.strChn

from ( select t1.strChn

from ( select '澳' strChn from sysibm.sysdummy1

union all

select '怖' strChn from sysibm.sysdummy1

union all

select '錯' strChn from sysibm.sysdummy1

union all

select '墮' strChn from sysibm.sysdummy1

union all

select '貳' strChn from sysibm.sysdummy1

union all

select '咐' strChn from sysibm.sysdummy1

union all

select '過' strChn from sysibm.sysdummy1

union all

select '禍' strChn from sysibm.sysdummy1

union all

select '禍' strChn from sysibm.sysdummy1

union all

select '駿' strChn from sysibm.sysdummy1

union all

select '闊' strChn from sysibm.sysdummy1

union all

select '絡' strChn from sysibm.sysdummy1

union all

select '穆' strChn from sysibm.sysdummy1

union all

select '諾' strChn from sysibm.sysdummy1

union all

select '漚' strChn from sysibm.sysdummy1

union all

select '瀑' strChn from sysibm.sysdummy1

union all

select '群' strChn from sysibm.sysdummy1

union all

select '弱' strChn from sysibm.sysdummy1

union all

select '所' strChn from sysibm.sysdummy1

union all

select '唾' strChn from sysibm.sysdummy1

union all

select '唾' strChn from sysibm.sysdummy1

union all

select '唾' strChn from sysibm.sysdummy1

union all

select '誤' strChn from sysibm.sysdummy1

union all

select '迅' strChn from sysibm.sysdummy1

union all

select '孕' strChn from sysibm.sysdummy1

union all

select '座' strChn from sysibm.sysdummy1

union all

select in_strChn strChn from sysibm.sysdummy1

) as t1

order by t1.strChn

) as t2

do

if ( strChn = in_strChn ) then

set chResult = chr( ascii('a') + ( case n when 26 then n-1 else n end ) );

return chResult;

end if;

set n = n + 1;

end for;

return chResult;

end@

實際使用中,應注意建立數據庫時字符集參數的設置,應使用GBK字符集。

應用以下命令查看已建立數據庫的字符集:

db2 connect to db_name user user_name using password

db2 get db cfg | grep -i 'code set'

此參數在數據庫建立之后不能修改。

標簽: DB2 數據庫
主站蜘蛛池模板: 一级毛片牲交大片 | 特级毛片8级毛片免费观看 特级毛片免费观看视频 | 乱人伦中文字幕视频 | 男人天堂国产 | 国产高清三级 | 亚洲天堂国产 | 日韩欧美一区二区中文字幕 | 国产亚洲精品午夜一区 | 免费在线观看毛片 | 日韩中文字幕在线观看 | 欧美高清强视频 | 日韩一级片免费在线观看 | 亚洲国产精品热久久2022 | 性日韩精品 | 国产亚洲欧美视频 | 国产一区二区三区国产精品 | 国产高清在线看 | 国产精品久久久久久免费 | 国产aaa级一级毛片 国产aaa毛片 | 欧美性视频一区二区三区 | 最新欧美精品一区二区三区 | 国产一区国产二区国产三区 | 欧美成人免费sss | 亚洲理论欧美理论在线观看 | 久久免费在线观看 | 国产一区二区三区高清 | 在线看欧美日韩中文字幕 | 国产精品免费一区二区三区四区 | 欧美一区永久视频免费观看 | 亚洲日韩精品欧美一区二区一 | 成人性视频在线 | 日本免费视频观看在线播放 | 久久777国产线看是看精品 | 欧美另类老妇 | 黑人边吃奶边扎下面激情视频 | 国产成人精品高清在线 | 在线观看亚洲天堂 | 久久亚洲精品成人综合 | 日韩国产欧美一区二区三区 | 美女很黄很黄免费 | 成人性生片全套 |