当前位置: 代码迷 >> Sql Server >> sql记录查询,高手帮解决一下!解决办法
  详细解决方案

sql记录查询,高手帮解决一下!解决办法

热度:91   发布时间:2016-04-27 15:37:38.0
sql记录查询,高手帮解决一下!!!
表   A   中有dabh字段,为varchar类型,
,如果我想   select   dabh   from   A   order   by   dabh   则显示
      dabh                                     而我最终想查询为:
---------               ---------
1           null                               1           null    
2           0                                     2           0    
3           1                                     3           1
4           10                                   4           10  
5           101                                 5           12
6           12                                   6           20
7           20                                   7           101  
8           ...                                 8           ...
100       a1                                   100       a1    
101       b1                                   101       b1    
xxx       ...                                 xxx       ...          

注:把null排到最前,然后把数字排到后面,最后把字母开头的放到最后,
        最后用一条SQL语句查询出来

        自己也做了一点用union连接:
    select   dabh   from   A   where   dabh   is   null   order   by   dabh  
union  
    select   dabh   from   A     where   not   dabh   is     null   and   dabh   not   like   '[a-z]% '   order   by   cast(dabh   as   int)  
union
    select   dabh   from   A     where   not   dabh   is     null   and   dabh   like   '[a-z]% '   order   by   dabh

    单用一句行,连接到一起就错。

    请高手指点一下。

------解决方案--------------------
try

Select * From A
Order By (Case When dabh Is Null Then 0 Else
  相关解决方案