sql按拼音排序 - sqlserver -

sql按拼音排序

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

19968       一
20008       丨
20022       丶
20031       丿
20032       乀
20033       乁
20057       乙
20058       乚
20059       乛
20101       亅
19969       丁
..........

  从上面的结果,我们可以清楚的看到,一笔的汉字,code是从19968到20101,从小到大排,但到
了二笔汉字的第一个字“丁”,code为19969,就不按顺序而重新开始了。有了这结果,我们就可以轻
松的用sql语句得到每种笔划汉字归类的第一个或最后一个汉字。
下面用语句得到最后一个汉字:

create table #t1(id int identity,code int,cnword nvarchar(2))

insert #t1(code,cnword)
select code,nchar(code) as cnword from #t
order by nchar(code) collate chinese_prc_stroke_cs_as_ks_ws,code


select a.cnword
from #t1 a
left join #t1 b on a.id=b.id-1 and a.code<b.code
where b.code is null
order by a.id

得到36个汉字,每个汉字都是每种笔划数按chinese_prc_stroke_cs_as_ks_ws排序规则排序后的
最后一个汉字:

亅阝马风龙齐龟齿鸩龀龛龂龆龈龊龍龠龎龐龑龡龢龝齹龣龥齈龞麷鸞麣龖龗齾齉龘

上面可以看出:“亅”是所有一笔汉字排序后的最后一个字,“阝”是所有二笔汉字排序后的最后
一个字......等等。
  但同时也发现,从第33个汉字“龗(33笔)”后面的笔划有些乱,不正确。但没关系,比“龗”笔划
多的只有四个汉字,我们手工加上:齾35笔,齉36笔,靐39笔,龘64笔

建汉字笔划表(tab_hzbh):
create table tab_hzbh(id int identity,cnword nchar(1))
--先插入前33个汉字
insert tab_hzbh
select top 33 a.cnword
from #t1 a
left join #t1 b on a.id=b.id-1 and a.code<b.code
where b.code is null
order by a.id
--再加最后四个汉字
set identity_insert tab_hzbh on
go
insert tab_hzbh(id,cnword)
     select 35,n齾
union all select 36,n齉
union all select 39,n靐
union all select 64,n龘
go
set identity_insert tab_hzbh off
go

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

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靐



相关热词搜索:

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