思想决定人生,态度改变一切

成功者找方法,失败者找借口! 做事先做人;安分做人,本分做事!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 

到此为止,我们可以得到结果了,比如我们想得到汉字“国”的笔划:

declare  @a  nchar(1)
            set  @a='国'
            select  top  1  id
            from    tab_hzbh
            where  cnword>=@a  collate  Chinese_PRC_Stroke_CS_AS_KS_WS
            order  by  id
            id
            -----------
            8
            (结果:汉字“国”笔划数为8)

上面所有准备过程,只是为了写下面这个函数,这个函数撇开上面建的所有临时表和固定表,为了通用和代码转移方便,把表tab_hzbh的内容写在语句内,然后计算用户输入一串汉字的总笔划:

create  function  fun_getbh(@str  nvarchar(4000))
            returns  int
            as
            begin
            declare  @word  nchar(1),@n  int
            set  @n=0
            while  len(@str)>0
            begin
            set  @word=left(@str,1)
            --如果非汉字,笔划当0计
            set  @n=@n+(case  when  unicode(@word)  between  19968  and  19968+20901
            then  (select  top  1  id  from  (
            select  1  as  id,N'亅'  as  word
            union  all  select  2,N'阝'
            union  all  select  3,N'马'
            union  all  select  4,N'风'
            union  all  select  5,N'龙'
            union  all  select  6,N'齐'
            union  all  select  7,N'龟'
            union  all  select  8,N'齿'
            union  all  select  9,N'鸩'
            union  all  select  10,N'龀'
            union  all  select  11,N'龛'
            union  all  select  12,N'龂'
            union  all  select  13,N'龆'
            union  all  select  14,N'龈'
            union  all  select  15,N'龊'
            union  all  select  16,N'龍'
            union  all  select  17,N'龠'
            union  all  select  18,N'龎'
            union  all  select  19,N'龐'
            union  all  select  20,N'龑'
            union  all  select  21,N'龡'
            union  all  select  22,N'龢'
            union  all  select  23,N'龝'
            union  all  select  24,N'齹'
            union  all  select  25,N'龣'
            union  all  select  26,N'龥'
            union  all  select  27,N'齈'
            union  all  select  28,N'龞'
            union  all  select  29,N'麷'
            union  all  select  30,N'鸞'
            union  all  select  31,N'麣'
            union  all  select  32,N'龖'
            union  all  select  33,N'龗'
            union  all  select  35,N'齾'
            union  all  select  36,N'齉'
            union  all  select  39,N'靐'
            union  all  select  64,N'龘'
            )  T
            where  word>=@word  collate  Chinese_PRC_Stroke_CS_AS_KS_WS
            order  by  id  ASC)  else  0  end)
            set  @str=right(@str,len(@str)-1)
            end
            return  @n
            end

函数调用实例:

select dbo.fun_getbh('中华人民共和国'),dbo.fun_getbh('中華人民共和國') 

执行结果:笔划总数分别为39和46,简繁体都行。

当然,你也可以把上面“UNION ALL”内的汉字和笔划改存在固定表内,在汉字列建CLUSTERED INDEX,列排序规则设定为:

Chinese_PRC_Stroke_CS_AS_KS_WS

这样速度更快。如果你用的是BIG5码的操作系统,你得另外生成汉字,方法一样。但有一点要记住:这些汉字是通过SQL语句Select出来的,不是手工输入的,更不是查字典得来的,因为新华字典毕竟不同于UNICODE字符集,查字典的结果会不正确。  

用排序规则的特性得到汉字拼音首字母

用得到笔划总数相同的方法,我们也可以写出求汉字拼音首字母的函数。如下:

create  function  fun_getPY(@str  nvarchar(4000))
            returns  nvarchar(4000)
            as
            begin
            declare  @word  nchar(1),@PY  nvarchar(4000)
            set  @PY=''
            while  len(@str)>0
            begin
            set  @word=left(@str,1)
            --如果非汉字字符,返回原字符
            set  @PY=@PY+(case  when  unicode(@word)  between  19968  and  19968+20901
            then  (select  top  1  PY  from  (
            select  'A'  as  PY,N'驁'  as  word
            union  all  select  'B',N'簿'
            union  all  select  'C',N'錯'
            union  all  select  'D',N'鵽'
            union  all  select  'E',N'樲'
            union  all  select  'F',N'鰒'
            union  all  select  'G',N'腂'
            union  all  select  'H',N'夻'
            union  all  select  'J',N'攈'
            union  all  select  'K',N'穒'
            union  all  select  'L',N'鱳'
            union  all  select  'M',N'旀'
            union  all  select  'N',N'桛'
            union  all  select  'O',N'漚'
            union  all  select  'P',N'曝'
            union  all  select  'Q',N'囕'
            union  all  select  'R',N'鶸'
            union  all  select  'S',N'蜶'
            union  all  select  'T',N'籜'
            union  all  select  'W',N'鶩'
            union  all  select  'X',N'鑂'
            union  all  select  'Y',N'韻'
            union  all  select  'Z',N'咗'
            )  T
            where  word>=@word  collate  Chinese_PRC_CS_AS_KS_WS
            order  by  PY  ASC)  else  @word  end)
            set  @str=right(@str,len(@str)-1)
            end
            return  @PY
            end

函数调用实例:

select dbo.fun_getPY('中华人民共和国'),dbo.fun_getPY('中華人民共和國')

结果都是:ZHRMGHG

大家如果有兴趣,可以使用相同的方法,扩展得到汉字全拼的函数,甚至你还可以得到全拼的读音声调,不过全拼分类大多了。得到全拼最好还是用对照表,两万多汉字搜索速度显然很快,另外,用对照表还可以充分利用表的索引。

原文出处:http://tech.ccidnet.com/art/1106/20080311/1386735_3.html

posted on 2008-06-24 18:21  投石问路  阅读(310)  评论(0编辑  收藏  举报