日期:2014-05-18  浏览次数:20461 次

排序问题SQL
譬如一个表
有两个字段
其中一个字段是编号
但是这个编号是有规律的
如下
091101-1
091101-2
091102
091103
091104
……
091199
101201
101202
101203
101204
……
1012100
我要求按照这个字段,譬如这个字段叫做 ph排序后会像上面一样,前四位是类似年月的什么0912,后面是从01~大于01的数字组合而成,而且后面的数字可能自带-1、-2,我该怎么排序?

------解决方案--------------------
SQL code
order by ph,replace(right(ph,len(ph)-7),'-','')

------解决方案--------------------
SQL code
order by left(ph,4),substring(ph,5,2),right(ph,len(ph)-charindex('-',ph))

------解决方案--------------------
lz给的数据和结果不明显
直接select * from tb order by ph就能得到你发的结果其实
你要说下规则啊
------解决方案--------------------
对有-的,没-的,什么都没的,怎么排序
------解决方案--------------------
SQL code


create table tab_a
(
    ph varchar(30)
)

delete from tab_a
insert into tab_a
select '091103' union all
select '091101-1' union all
select '091104' union all
select '091102' union all
select '091101-2' 

select *from tab_a order by ph
--我自己建立个表存放测试数据  数据的顺序我刻意错乱 插入
--查询只需要 order by ph 就行了

------解决方案--------------------
SQL code

补充下查询结果

091101-1
091101-2
091102
091103
091104

------解决方案--------------------
SQL code
SELECT * FROM DBO.TB ORDER BY LEFT(ph,6)

------解决方案--------------------
select *from tab_a order by left(ph,6),right(ph,len(ph)-charindex('-',ph))