遇到个小问题,请教大家:
数据库里根据类别排序,比如说 CD1、CD2、CD10、CD21...,希望的结果是 CD1、CD2、CD10、CD21 ,但实际是:CD1、CD10、CD2、CD21...,请教该怎样进行排序呢?谢谢
数据库里根据类别排序,比如说 CD1、CD2、CD10、CD21...,希望的结果是 CD1、CD2、CD10、CD21 ,但实际是:CD1、CD10、CD2、CD21...,请教该怎样进行排序呢?谢谢
FROM tb
ORDER BY LEFT(col, PATINDEX('%[0-9]%', col)-1),
STUFF(col, 1, PATINDEX('%[0-9]%', col)-1, '')*1
create table test_2(id int,c1 varchar(10))insert into test_2
select 1,'cd1'
union
select 2,'cd2'
union
select 3,'cd10'
union
select 4,'cd21'
union
select 5,'cd20'select * from test_2 order by cast(ltrim(replace(c1,'cd',' ')) as int)--1 cd1
--2 cd2
--3 cd10
--5 cd20
--4 cd21
编码设计的时候就要考虑周全,CD01、CD02就不会有这个问题了。否则就采用LS各位的方案。