22,209
社区成员
发帖
与我相关
我的任务
分享
create table tb (BH varchar(5))
insert into tb
select 'C1-01' union all
select 'C10-1' union all
select 'C10-2' union all
select 'C11-1' union all
select 'C1-02'
go
select * from tb order by cast(replace(left(bh , charindex('-',bh)-1),'C','') as int) , cast(substring(bh , charindex('-',bh)+1 , len(bh)) as int)
drop table tb
/*
BH
-----
C1-01
C1-02
C10-1
C10-2
C11-1
(所影响的行数为 5 行)
*/
select * from [table1] order by cast(substring(bh,2,charindex('-',bh)-2) as int),
cast(substring(bh,charindex('-',bh)+1,len(bh)) as int)
--借用2楼测试数据:
--> 测试数据: [table1]
if object_id('[table1]') is not null drop table [table1]
go
create table [table1] (BH varchar(20))
insert into [table1]
select 'C1-01' union all
select 'C10-1' union all
select 'C10-2' union all
select 'C11-1' union all
select 'C1-02'
select * from [table1] order by parsename(stuff(replace(bh,'-','.'),1,1,''),2)*1,parsename(replace(bh,'-','.'),1)*1
BH
--------------------
C1-01
C1-02
C10-1
C10-2
C11-1
(5 行受影响)
/*
C1-01
C1-02
C10-1
C10-2
C11-1
*/
declare @sql varchar(100)
set @sql=N'C1-01,C1-02,C10-1,C10-2,C11-1'
select * from table1
order by charindex(N','+BH+N',',N','+@sql+N',')
--(1)借用字符串进行无规律排序
create table #DepartMent
(
Depart varchar(10)
)
insert into #DepartMent select '组长'
union all select '助理'
union all select '总经理'
union all select '员工'
union all select '副总经理'
union all select '主管'
declare @sql varchar(100)
set @sql=N'总经理,副总经理,主管,组长,助理,员工'
select * from #DepartMent
order by charindex(N','+Depart+N',',N','+@sql+N',')
select * from #
order by
ascii(substring(bh,1,1)),
ascii(substring(bh,2,1)),
ascii(substring(bh,3,1)),
ascii(substring(bh,4,1)),
ascii(substring(bh,5,1))
--> 测试数据: [table1]
if object_id('[table1]') is not null drop table [table1]
create table [table1] (BH varchar(5))
insert into [table1]
select 'C1-01' union all
select 'C10-1' union all
select 'C10-2' union all
select 'C11-1' union all
select 'C1-02'
select * from [table1] order by cast(substring(bh,2,charindex('-',bh)-2) as int),
cast(substring(bh,charindex('-',bh)+1,len(bh)) as int)