惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
D
Docker
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
月光博客
月光博客
小众软件
小众软件
量子位
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
罗磊的独立博客
博客园 - 叶小钗
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
博客园 - 司徒正美
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
GbyAI
GbyAI
C
Check Point Blog

博客园 - 投石问路

ASP做贝宝(paypal)支付接口(推荐)[转] - 投石问路 - 博客园 asp paypal接口处理 - 投石问路 - 博客园 Asp.Net 学习资源列表 推荐几个.NET开源图表组件 机器克隆会导致DTC进程堵死 [SQLServer2005]ADO.NET2.0中的查询通知 SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ] -- [转] 讲解SQL 2000的Collection排序规则 (2)[转] 讲解SQL 2000的Collection排序规则 (1)[转] IIS出现server application error,解决方案一 C#动态获取当前屏幕中光标所在位置的颜色 Excel开发(VSTO2005):简化工作表中选定区域的操作。[轉載] .NET 2.0 CER学习笔记 [转载] 格式化类型连接地址 FLV Flash video streaming with ASP.NET 2.0, IIS and HTTP handler [转载] ASP.NET 视频截图功能的C#代码[转载] NET 2.0(C#)调用ffmpeg处理视频的方法 利用Mencoder转换视频格式[转载] FLV播放器 源码二例[转载]
讲解SQL 2000的Collection排序规则 (3)[转]
投石问路 · 2008-06-24 · via 博客园 - 投石问路

 

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

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