- 爱易网页
 
                        - 
                            MSSQL教程
 
                        - 一个排序规则的有关问题, 
 
                         
                    
                    
                    日期:2014-05-18  浏览次数:20646 次 
                    
                        
                         一个排序规则的问题,急,急!!!
题目如下: 
 编号            序号            是否                     图号 
 1429	1	出图	AWwpf.11.1	 
 1430	1	出图	AWwpf.11.1-1	 
 1436	12	出图	AWwpf.11.1-12	 
 1431	2	出图	AWwpf.11.1-2	 
 1432	3	出图	AWwpf.11.1-3	 
 1433	5	出图	AWwpf.11.1-5	 
 1434	6	出图	AWwpf.11.1-6	 
 1435	9	出图	AWwpf.11.1-9	 
 1437	2	出图	AWwpf.11.2	 
 1438	3	出图	AWwpf.11.2-3	 
 1445	9	出图	AWwpf.11-10	 
 1446	10	出图	AWwpf.11-11	 
 1447	11	出图	AWwpf.11-12	 
 1449	13	出图	AWwpf.11-14	 
 1450	14	出图	AWwpf.11-15	 
 1451	15	出图	AWwpf.11-16	 
 1440	4	出图	AWwpf.11-4	 
 1442	6	出图	AWwpf.11-6	 
 1443	7	出图	AWwpf.11-7	 
 1444	8	出图	AWwpf.11-8 
  
 SQl=select   *   from   T1   order   by   图号 
 这是按照图号排序的结果,现在要求实现按编号排序的结果   也就是下图 
  
 1429	1	出图	AWwpf.11.1	 
 1430	1	出图	AWwpf.11.1-1	 
 1431	2	出图	AWwpf.11.1-2	 
 1432	3	出图	AWwpf.11.1-3	 
 1433	5	出图	AWwpf.11.1-5	 
 1434	6	出图	AWwpf.11.1-6	 
 1435	9	出图	AWwpf.11.1-9	 
 1436	12	出图	AWwpf.11.1-12	 
 1437	2	出图	AWwpf.11.2	 
 1438	3	出图	AWwpf.11.2-3	 
 1440	4	出图	AWwpf.11-4	 
 1442	6	出图	AWwpf.11-6	 
 1443	7	出图	AWwpf.11-7	 
 1444	8	出图	AWwpf.11-8	 
 1445	9	出图	AWwpf.11-10	 
 1446	10	出图	AWwpf.11-11	 
 1447	11	出图	AWwpf.11-12	 
 1449	13	出图	AWwpf.11-14	 
 1450	14	出图	AWwpf.11-15	 
 1451	15	出图	AWwpf.11-16	 
  
 SQl=select   *   from   T1   order   by   序号 
  
 在本表中   AWwpf.11   是一个参数   ,且参数长度不固定,有什么样的方法能 
 够实现一个通用的排序规则?排序规则要求   优先考虑    "   . "      后考虑    "- " 
  
------解决方案--------------------
先按第一列,如果重复按第二列,依次类推!不知道你是怎么样存储的?
------解决方案--------------------
好像你理解的次序不对 
  
  
 declare @t table( 
 图号 varchar(20) 
 ) 
 insert @t select 
  'AWwpf.11.1 ' 
 union all select 
  'AWwpf.11.1-1 ' 
 union all select 
  'AWwpf.11.1-12 ' 
 union all select 
  'AWwpf.11.1-2 ' 
 union all select 
  'AWwpf.11.1-3 ' 
 union all select 
  'AWwpf.11.1-5 ' 
 union all select 
  'AWwpf.11.1-6 ' 
 union all select 
  'AWwpf.11.1-9 ' 
 union all select 
  'AWwpf.11.2 ' 
 union all select 
  'AWwpf.11.2-3 ' 
 union all select 
  'AWwpf.11-10 ' 
 union all select 
  'AWwpf.11-11 ' 
 union all select 
  'AWwpf.11-12 ' 
 union all select 
  'AWwpf.11-14 ' 
 union all select 
  'AWwpf.11-15 ' 
 union all select 
  'AWwpf.11-16 ' 
 union all select 
  'AWwpf.11-4 ' 
 union all select 
  'AWwpf.11-6 ' 
 union all select 
  'AWwpf.11-7 ' 
 union all select 
  'AWwpf.11-8 ' 
  
 select * 
 from @t  
 order by  
 case when charindex( '- ',图号)> 0 then REVERSE(stuff(REVERSE(图号),1,charINDEX( '- ',REVERSE(图号)), ' ')) else 图号 end, 
 case when charindex( '- ',图号)> 0 then cast(REVERSE(left(REVERSE(图号),charINDEX( '- ',REVERSE(图号))-1)) as int) else 0 end 
  
 --结果 
 图号                    
 --------------------  
 AWwpf.11-4 
 AWwpf.11-6 
 AWwpf.11-7 
 AWwpf.11-8 
 AWwpf.11-10 
 AWwpf.11-11 
 AWwpf.11-12 
 AWwpf.11-14 
 AWwpf.11-15 
 AWwpf.11-16 
 AWwpf.11.1 
 AWwpf.11.1-1 
 AWwpf.11.1-2 
 AWwpf.11.1-3 
 AWwpf.11.1-5 
 AWwpf.11.1-6 
 AWwpf.11.1-9 
 AWwpf.11.1-12 
 AWwpf.11.2 
 AWwpf.11.2-3