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

其运行结果如下图:

原文地址:https://www.cnblogs.com/Chaser-Eagle/p/3684815.html