SQL按汉语拼音首字母排序

以常用到的省的数据表(province)为例,其中name字段为省的名称,SQL语句如下:

 1 select   isnull(b.py,upper(left(a.name,1)))     as   py  
 2   ,a.name  
 3   from   province
 4   a  
 5   left   outer   join   (  
 6   select   A   as   PY,N   as   word,   N   as   sword  
 7   union   select   B,N簿,N  
 8   union   select   C,N,N簿  
 9   union   select   D,N,N  
10   union   select   E,N,N  
11   union   select   F,N,N  
12   union   select   G,N,N  
13   union   select   H,N,N  
14   union   select   J,N,N  
15   union   select   K,N,N  
16   union   select   L,N,N  
17   union   select   M,N,N  
18   union   select   N,N,N  
19   union   select   O,N,N  
20   union   select   P,N,N  
21   union   select   Q,N,N  
22   union   select   R,N,N  
23   union   select   S,N,N  
24   union   select   T,N,N  
25   union   select   W,N,N  
26   union   select   X,N,N  
27   union   select   Y,N,N  
28   union   select   Z,N,N  
29   )   b   on   left(name,1)   between   b.sword   and   b.word order by name

其运行结果如下图:

SQL按汉语拼音首字母排序,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。