sql按拼音排序 - sqlserver -

sql按拼音排序

时间:2010-01-27 00:06:46   来源:   评论:加载中...   点击:加载中...
sql按拼音排序select * from user order by name collate Chinese_PRC_CS_AS_KS_WS二.排序规则简介: 什么叫排序规则呢?ms是这样...

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

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



相关热词搜索:

 
上一篇:sql取分组的前几条/指定条数
下一篇:sql开启OpenRowset/OpenDatasource的办法
收藏 将此文推荐给朋友
分享到: