110,525
社区成员
发帖
与我相关
我的任务
分享
;with cte_a(someName) as
(
select '三环1段' union all
select '三环2段' union all
select '三环5段' union all
select '三环6段' union all
select '四环11段' union all
select '四环12段' union all
select '四环13段' union all
select '三环7段' union all
select '三环8段' union all
select '三环3段' union all
select '三环4段' union all
select '三环9段' union all
select '三环10段' union all
select '四环5段' union all
select '四环6段' union all
select '四环7段' union all
select '四环8段' union all
select '三环11段' union all
select '三环12段' union all
select '四环1段' union all
select '四环2段' union all
select '四环3段' union all
select '四环4段' union all
select '四环9段' union all
select '五环30段' union all
select '五环100段' union all
select '五环9段' union all
select '四环10段' union all
select '四环14段' union all
select '四环15段'
)
,cte_b as(
select someName, SUBSTRING(someName,3,1)+case when ISNUMERIC(SUBSTRING(someName,4,1))=1 then SUBSTRING(someName,4,1) else '' end as Col
from cte_a)
select * from cte_b order by SUBSTRING(someName,1,2),convert(int,Col) ASC
--结果
someName Col
--------- ----
三环1段 1
三环2段 2
三环3段 3
三环4段 4
三环5段 5
三环6段 6
三环7段 7
三环8段 8
三环9段 9
三环10段 10
三环11段 11
三环12段 12
四环1段 1
四环2段 2
四环3段 3
四环4段 4
四环5段 5
四环6段 6
四环7段 7
四环8段 8
四环9段 9
四环10段 10
四环11段 11
四环12段 12
四环13段 13
四环14段 14
四环15段 15
五环9段 9
五环100段 10
五环30段 30
(30 行受影响)
declare @x nvarchar(10)
declare @d nvarchar(10)
set @x='环'
set @d='段'
;with cte_a(someName) as
(
select '三环1段' union all
select '三环2段' union all
select '三环5段' union all
select '三环6段' union all
select '四环11段' union all
select '四环12段' union all
select '四环13段' union all
select '三环7段' union all
select '三环8段' union all
select '三环3段' union all
select '三环4段' union all
select '三环9段' union all
select '三环10段' union all
select '四环5段' union all
select '四环6段' union all
select '四环7段' union all
select '四环8段' union all
select '三环11段' union all
select '三环12段' union all
select '四环1段' union all
select '四环2段' union all
select '四环3段' union all
select '四环4段' union all
select '四环9段' union all
select '五环30段' union all
select '五环100段' union all
select '五环9段' union all
select '四环10段' union all
select '四环14段' union all
select '四环15段'
)
,
cte_b(huan,d,duan) as(
select left(someName,charindex(@x,someName)) ,cast(substring(someName,charindex(@x,someName)+1,LEN(someName)-charindex(@x,someName)-1) as int),@d from cte_a
)
select huan+cast(d as nvarchar(10))+duan as someName from cte_b order by huan,d
--结果
someName
-----------------------------
三环1段
三环2段
三环3段
三环4段
三环5段
三环6段
三环7段
三环8段
三环9段
三环10段
三环11段
三环12段
四环1段
四环2段
四环3段
四环4段
四环5段
四环6段
四环7段
四环8段
四环9段
四环10段
四环11段
四环12段
四环13段
四环14段
四环15段
五环9段
五环30段
五环100段
(30 行受影响)